blob: 2cf376e0136c5f73bef08c267af0c66e0e4c3785 [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"
Evan Chenge76a33b2011-07-20 05:58:47 +000026#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000027#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000028#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000029#include "llvm/MC/MCSymbol.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000030#include "llvm/MC/MCDwarf.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000031#include "llvm/Support/CommandLine.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000032#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000033#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000034#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000035#include "llvm/Support/raw_ostream.h"
Roman Divacky54b0f4f2011-01-27 17:16:37 +000036#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000037#include "llvm/Target/TargetAsmParser.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000038#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000039#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000040using namespace llvm;
41
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000042static cl::opt<bool>
43FatalAssemblerWarnings("fatal-assembler-warnings",
44 cl::desc("Consider warnings as error"));
45
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000046namespace {
47
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000048/// \brief Helper class for tracking macro definitions.
49struct Macro {
50 StringRef Name;
51 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000052 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000053
54public:
Rafael Espindola65366442011-06-05 02:43:45 +000055 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
56 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000057};
58
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000059/// \brief Helper class for storing information about an active macro
60/// instantiation.
61struct MacroInstantiation {
62 /// The macro being instantiated.
63 const Macro *TheMacro;
64
65 /// The macro instantiation with substitutions.
66 MemoryBuffer *Instantiation;
67
68 /// The location of the instantiation.
69 SMLoc InstantiationLoc;
70
71 /// The location where parsing should resume upon instantiation completion.
72 SMLoc ExitLoc;
73
74public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000075 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000076 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000077};
78
Daniel Dunbaraef87e32010-07-18 18:31:38 +000079/// \brief The concrete assembly parser instance.
80class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000081 friend class GenericAsmParser;
82
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
84 void operator=(const AsmParser &); // DO NOT IMPLEMENT
85private:
86 AsmLexer Lexer;
87 MCContext &Ctx;
88 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000089 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000090 SourceMgr &SrcMgr;
91 MCAsmParserExtension *GenericParser;
92 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000093
Daniel Dunbaraef87e32010-07-18 18:31:38 +000094 /// This is the current buffer index we're lexing from as managed by the
95 /// SourceMgr object.
96 int CurBuffer;
97
98 AsmCond TheCondState;
99 std::vector<AsmCond> TheCondStack;
100
101 /// DirectiveMap - This is a table handlers for directives. Each handler is
102 /// invoked after the directive identifier is read and is responsible for
103 /// parsing and validating the rest of the directive. The handler is passed
104 /// in the directive name and the location of the directive keyword.
105 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000106
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000107 /// MacroMap - Map of currently defined macros.
108 StringMap<Macro*> MacroMap;
109
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000110 /// ActiveMacros - Stack of active macro instantiations.
111 std::vector<MacroInstantiation*> ActiveMacros;
112
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000113 /// Boolean tracking whether macro substitution is enabled.
114 unsigned MacrosEnabled : 1;
115
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000116 /// Flag tracking whether any errors have been encountered.
117 unsigned HadError : 1;
118
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000119public:
120 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
121 const MCAsmInfo &MAI);
122 ~AsmParser();
123
124 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
125
126 void AddDirectiveHandler(MCAsmParserExtension *Object,
127 StringRef Directive,
128 DirectiveHandler Handler) {
129 DirectiveMap[Directive] = std::make_pair(Object, Handler);
130 }
131
132public:
133 /// @name MCAsmParser Interface
134 /// {
135
136 virtual SourceMgr &getSourceManager() { return SrcMgr; }
137 virtual MCAsmLexer &getLexer() { return Lexer; }
138 virtual MCContext &getContext() { return Ctx; }
139 virtual MCStreamer &getStreamer() { return Out; }
140
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000141 virtual bool Warning(SMLoc L, const Twine &Msg);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000142 virtual bool Error(SMLoc L, const Twine &Msg);
143
144 const AsmToken &Lex();
145
146 bool ParseExpression(const MCExpr *&Res);
147 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
148 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
149 virtual bool ParseAbsoluteExpression(int64_t &Res);
150
151 /// }
152
153private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000154 void CheckForValidSection();
155
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000156 bool ParseStatement();
157
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000158 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000159 bool expandMacro(SmallString<256> &Buf, StringRef Body,
160 const std::vector<StringRef> &Parameters,
161 const std::vector<std::vector<AsmToken> > &A,
162 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000163 void HandleMacroExit();
164
165 void PrintMacroInstantiations();
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000166 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type,
167 bool ShowLine = true) const {
168 SrcMgr.PrintMessage(Loc, Msg, Type, ShowLine);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000169 }
170
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000171 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
172 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000173
174 /// \brief Reset the current lexer position to that given by \arg Loc. The
175 /// current token is not set; clients should ensure Lex() is called
176 /// subsequently.
177 void JumpToLoc(SMLoc Loc);
178
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000179 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000180
181 /// \brief Parse up to the end of statement and a return the contents from the
182 /// current token until the end of the statement; the current token on exit
183 /// will be either the EndOfStatement or EOF.
184 StringRef ParseStringToEndOfStatement();
185
Nico Weber4c4c7322011-01-28 03:04:41 +0000186 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000187
188 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
189 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
190 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000191 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000192
193 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
194 /// and set \arg Res to the identifier contents.
195 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000196
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000197 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000198
199 // ".ascii", ".asciiz", ".string"
200 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000201 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000202 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000203 bool ParseDirectiveFill(); // ".fill"
204 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000205 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000206 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000207 bool ParseDirectiveOrg(); // ".org"
208 // ".align{,32}", ".p2align{,w,l}"
209 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
210
211 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
212 /// accepts a single symbol (which should be a label or an external).
213 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000214
215 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
216
217 bool ParseDirectiveAbort(); // ".abort"
218 bool ParseDirectiveInclude(); // ".include"
219
220 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000221 // ".ifdef" or ".ifndef", depending on expect_defined
222 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000223 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
224 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
225 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
226
227 /// ParseEscapedString - Parse the current token as a string which may include
228 /// escaped characters and return the string contents.
229 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000230
231 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
232 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000233};
234
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000235/// \brief Generic implementations of directive handling, etc. which is shared
236/// (or the default, at least) for all assembler parser.
237class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000238 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
239 void AddDirectiveHandler(StringRef Directive) {
240 getParser().AddDirectiveHandler(this, Directive,
241 HandleDirective<GenericAsmParser, Handler>);
242 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000243public:
244 GenericAsmParser() {}
245
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000246 AsmParser &getParser() {
247 return (AsmParser&) this->MCAsmParserExtension::getParser();
248 }
249
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000250 virtual void Initialize(MCAsmParser &Parser) {
251 // Call the base implementation.
252 this->MCAsmParserExtension::Initialize(Parser);
253
254 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000255 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
256 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
257 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000258 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000259
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000260 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000261 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
262 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000263 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
264 ".cfi_startproc");
265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
266 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
268 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000269 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
270 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000271 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
272 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000273 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
274 ".cfi_def_cfa_register");
275 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
276 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000277 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
278 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000279 AddDirectiveHandler<
280 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
281 AddDirectiveHandler<
282 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000283 AddDirectiveHandler<
284 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
285 AddDirectiveHandler<
286 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000287 AddDirectiveHandler<
288 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000289
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000290 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000291 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
292 ".macros_on");
293 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
294 ".macros_off");
295 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
296 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
297 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000298
299 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
300 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000301 }
302
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000303 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
304
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000305 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
306 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
307 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000308 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000309 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000310 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
311 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000312 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000313 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000314 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000315 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
316 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000317 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000318 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000319 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
320 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000321 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000322
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000323 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000324 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
325 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000326
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000327 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000328};
329
330}
331
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000332namespace llvm {
333
334extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000335extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000336extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000337
338}
339
Chris Lattneraaec2052010-01-19 19:46:13 +0000340enum { DEFAULT_ADDRSPACE = 0 };
341
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000342AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
343 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000344 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000345 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000346 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000347 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000348
349 // Initialize the generic parser.
350 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000351
352 // Initialize the platform / file format parser.
353 //
354 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
355 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000356 if (_MAI.hasMicrosoftFastStdCallMangling()) {
357 PlatformParser = createCOFFAsmParser();
358 PlatformParser->Initialize(*this);
359 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000360 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000361 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000362 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000363 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000364 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000365 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000366}
367
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000368AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000369 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
370
371 // Destroy any macros.
372 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
373 ie = MacroMap.end(); it != ie; ++it)
374 delete it->getValue();
375
Daniel Dunbare4749702010-07-12 18:12:02 +0000376 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000377 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000378}
379
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000380void AsmParser::PrintMacroInstantiations() {
381 // Print the active macro instantiation stack.
382 for (std::vector<MacroInstantiation*>::const_reverse_iterator
383 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
384 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
385 "note");
386}
387
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000388bool AsmParser::Warning(SMLoc L, const Twine &Msg) {
389 if (FatalAssemblerWarnings)
390 return Error(L, Msg);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000391 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000392 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000393 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000394}
395
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000396bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000397 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000398 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000399 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000400 return true;
401}
402
Sean Callananfd0b0282010-01-21 00:19:58 +0000403bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000404 std::string IncludedFile;
405 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000406 if (NewBuf == -1)
407 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000408
Sean Callananfd0b0282010-01-21 00:19:58 +0000409 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000410
Sean Callananfd0b0282010-01-21 00:19:58 +0000411 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000412
Sean Callananfd0b0282010-01-21 00:19:58 +0000413 return false;
414}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000415
416void AsmParser::JumpToLoc(SMLoc Loc) {
417 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
418 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
419}
420
Sean Callananfd0b0282010-01-21 00:19:58 +0000421const AsmToken &AsmParser::Lex() {
422 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000423
Sean Callananfd0b0282010-01-21 00:19:58 +0000424 if (tok->is(AsmToken::Eof)) {
425 // If this is the end of an included file, pop the parent file off the
426 // include stack.
427 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
428 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000429 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000430 tok = &Lexer.Lex();
431 }
432 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000433
Sean Callananfd0b0282010-01-21 00:19:58 +0000434 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000435 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000436
Sean Callananfd0b0282010-01-21 00:19:58 +0000437 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000438}
439
Chris Lattner79180e22010-04-05 23:15:42 +0000440bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000441 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000442 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000443 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000444
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000445 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000446 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000447
448 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000449 AsmCond StartingCondState = TheCondState;
450
Chris Lattnerb717fb02009-07-02 21:53:43 +0000451 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000452 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000453 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000454
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000455 // We had an error, validate that one was emitted and recover by skipping to
456 // the next line.
457 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000458 EatToEndOfStatement();
459 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000460
461 if (TheCondState.TheCond != StartingCondState.TheCond ||
462 TheCondState.Ignore != StartingCondState.Ignore)
463 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000464
465 // Check to see there are no empty DwarfFile slots.
466 const std::vector<MCDwarfFile *> &MCDwarfFiles =
467 getContext().getMCDwarfFiles();
468 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000469 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000470 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000471 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000472
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000473 // Check to see that all assembler local symbols were actually defined.
474 // Targets that don't do subsections via symbols may not want this, though,
475 // so conservatively exclude them. Only do this if we're finalizing, though,
476 // as otherwise we won't necessarilly have seen everything yet.
477 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
478 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
479 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
480 e = Symbols.end();
481 i != e; ++i) {
482 MCSymbol *Sym = i->getValue();
483 // Variable symbols may not be marked as defined, so check those
484 // explicitly. If we know it's a variable, we have a definition for
485 // the purposes of this check.
486 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
487 // FIXME: We would really like to refer back to where the symbol was
488 // first referenced for a source location. We need to add something
489 // to track that. Currently, we just point to the end of the file.
490 PrintMessage(getLexer().getLoc(), "assembler local symbol '" +
491 Sym->getName() + "' not defined", "error", false);
492 }
493 }
494
495
Chris Lattner79180e22010-04-05 23:15:42 +0000496 // Finalize the output stream if there are no errors and if the client wants
497 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000498 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000499 Out.Finish();
500
Chris Lattnerb717fb02009-07-02 21:53:43 +0000501 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000502}
503
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000504void AsmParser::CheckForValidSection() {
505 if (!getStreamer().getCurrentSection()) {
506 TokError("expected section directive before assembly directive");
507 Out.SwitchSection(Ctx.getMachOSection(
508 "__TEXT", "__text",
509 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
510 0, SectionKind::getText()));
511 }
512}
513
Chris Lattner2cf5f142009-06-22 01:29:09 +0000514/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
515void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000516 while (Lexer.isNot(AsmToken::EndOfStatement) &&
517 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000518 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000519
Chris Lattner2cf5f142009-06-22 01:29:09 +0000520 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000521 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000522 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000523}
524
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000525StringRef AsmParser::ParseStringToEndOfStatement() {
526 const char *Start = getTok().getLoc().getPointer();
527
528 while (Lexer.isNot(AsmToken::EndOfStatement) &&
529 Lexer.isNot(AsmToken::Eof))
530 Lex();
531
532 const char *End = getTok().getLoc().getPointer();
533 return StringRef(Start, End - Start);
534}
Chris Lattnerc4193832009-06-22 05:51:26 +0000535
Chris Lattner74ec1a32009-06-22 06:32:03 +0000536/// ParseParenExpr - Parse a paren expression and return it.
537/// NOTE: This assumes the leading '(' has already been consumed.
538///
539/// parenexpr ::= expr)
540///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000541bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000542 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000543 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000544 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000545 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000546 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000547 return false;
548}
Chris Lattnerc4193832009-06-22 05:51:26 +0000549
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000550/// ParseBracketExpr - Parse a bracket expression and return it.
551/// NOTE: This assumes the leading '[' has already been consumed.
552///
553/// bracketexpr ::= expr]
554///
555bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
556 if (ParseExpression(Res)) return true;
557 if (Lexer.isNot(AsmToken::RBrac))
558 return TokError("expected ']' in brackets expression");
559 EndLoc = Lexer.getLoc();
560 Lex();
561 return false;
562}
563
Chris Lattner74ec1a32009-06-22 06:32:03 +0000564/// ParsePrimaryExpr - Parse a primary expression and return it.
565/// primaryexpr ::= (parenexpr
566/// primaryexpr ::= symbol
567/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000568/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000569/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000570bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000571 switch (Lexer.getKind()) {
572 default:
573 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000574 // If we have an error assume that we've already handled it.
575 case AsmToken::Error:
576 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000577 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000578 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000579 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000580 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000581 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000582 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000583 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000584 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000585 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000586 EndLoc = Lexer.getLoc();
587
588 StringRef Identifier;
589 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000590 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000591
Daniel Dunbarfffff912009-10-16 01:34:54 +0000592 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000593 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000594 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000595
596 // Lookup the symbol variant if used.
597 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000598 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000599 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000600 if (Variant == MCSymbolRefExpr::VK_Invalid) {
601 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000602 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000603 }
604 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000605
Daniel Dunbarfffff912009-10-16 01:34:54 +0000606 // If this is an absolute variable reference, substitute it now to preserve
607 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000608 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000609 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000610 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000611
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000612 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000613 return false;
614 }
615
616 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000617 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000618 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000619 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000620 case AsmToken::Integer: {
621 SMLoc Loc = getTok().getLoc();
622 int64_t IntVal = getTok().getIntVal();
623 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000624 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000625 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000626 // Look for 'b' or 'f' following an Integer as a directional label
627 if (Lexer.getKind() == AsmToken::Identifier) {
628 StringRef IDVal = getTok().getString();
629 if (IDVal == "f" || IDVal == "b"){
630 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
631 IDVal == "f" ? 1 : 0);
632 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
633 getContext());
634 if(IDVal == "b" && Sym->isUndefined())
635 return Error(Loc, "invalid reference to undefined symbol");
636 EndLoc = Lexer.getLoc();
637 Lex(); // Eat identifier.
638 }
639 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000640 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000641 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000642 case AsmToken::Real: {
643 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000644 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000645 Res = MCConstantExpr::Create(IntVal, getContext());
646 Lex(); // Eat token.
647 return false;
648 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000649 case AsmToken::Dot: {
650 // This is a '.' reference, which references the current PC. Emit a
651 // temporary label to the streamer and refer to it.
652 MCSymbol *Sym = Ctx.CreateTempSymbol();
653 Out.EmitLabel(Sym);
654 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
655 EndLoc = Lexer.getLoc();
656 Lex(); // Eat identifier.
657 return false;
658 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000659 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000660 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000661 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000662 case AsmToken::LBrac:
663 if (!PlatformParser->HasBracketExpressions())
664 return TokError("brackets expression not supported on this target");
665 Lex(); // Eat the '['.
666 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000667 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000668 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000669 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000670 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000671 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000672 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000673 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000674 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000675 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000676 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000677 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000678 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000679 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000680 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000681 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000682 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000683 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000684 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000685 }
686}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000687
Chris Lattnerb4307b32010-01-15 19:28:38 +0000688bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000689 SMLoc EndLoc;
690 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000691}
692
Daniel Dunbarcceba832010-09-17 02:47:07 +0000693const MCExpr *
694AsmParser::ApplyModifierToExpr(const MCExpr *E,
695 MCSymbolRefExpr::VariantKind Variant) {
696 // Recurse over the given expression, rebuilding it to apply the given variant
697 // if there is exactly one symbol.
698 switch (E->getKind()) {
699 case MCExpr::Target:
700 case MCExpr::Constant:
701 return 0;
702
703 case MCExpr::SymbolRef: {
704 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
705
706 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
707 TokError("invalid variant on expression '" +
708 getTok().getIdentifier() + "' (already modified)");
709 return E;
710 }
711
712 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
713 }
714
715 case MCExpr::Unary: {
716 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
717 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
718 if (!Sub)
719 return 0;
720 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
721 }
722
723 case MCExpr::Binary: {
724 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
725 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
726 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
727
728 if (!LHS && !RHS)
729 return 0;
730
731 if (!LHS) LHS = BE->getLHS();
732 if (!RHS) RHS = BE->getRHS();
733
734 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
735 }
736 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000737
738 assert(0 && "Invalid expression kind!");
739 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000740}
741
Chris Lattner74ec1a32009-06-22 06:32:03 +0000742/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000743///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000744/// expr ::= expr +,- expr -> lowest.
745/// expr ::= expr |,^,&,! expr -> middle.
746/// expr ::= expr *,/,%,<<,>> expr -> highest.
747/// expr ::= primaryexpr
748///
Chris Lattner54482b42010-01-15 19:39:23 +0000749bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000750 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000751 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000752 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
753 return true;
754
Daniel Dunbarcceba832010-09-17 02:47:07 +0000755 // As a special case, we support 'a op b @ modifier' by rewriting the
756 // expression to include the modifier. This is inefficient, but in general we
757 // expect users to use 'a@modifier op b'.
758 if (Lexer.getKind() == AsmToken::At) {
759 Lex();
760
761 if (Lexer.isNot(AsmToken::Identifier))
762 return TokError("unexpected symbol modifier following '@'");
763
764 MCSymbolRefExpr::VariantKind Variant =
765 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
766 if (Variant == MCSymbolRefExpr::VK_Invalid)
767 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
768
769 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
770 if (!ModifiedRes) {
771 return TokError("invalid modifier '" + getTok().getIdentifier() +
772 "' (no symbols present)");
773 return true;
774 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000775
Daniel Dunbarcceba832010-09-17 02:47:07 +0000776 Res = ModifiedRes;
777 Lex();
778 }
779
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000780 // Try to constant fold it up front, if possible.
781 int64_t Value;
782 if (Res->EvaluateAsAbsolute(Value))
783 Res = MCConstantExpr::Create(Value, getContext());
784
785 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000786}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000787
Chris Lattnerb4307b32010-01-15 19:28:38 +0000788bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000789 Res = 0;
790 return ParseParenExpr(Res, EndLoc) ||
791 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000792}
793
Daniel Dunbar475839e2009-06-29 20:37:27 +0000794bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000795 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000796
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000797 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000798 if (ParseExpression(Expr))
799 return true;
800
Daniel Dunbare00b0112009-10-16 01:57:52 +0000801 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000802 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000803
804 return false;
805}
806
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000807static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000808 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000809 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000810 default:
811 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000812
Daniel Dunbarcceba832010-09-17 02:47:07 +0000813 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000814 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000815 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000816 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000817 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000818 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000819 return 1;
820
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000821
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000822 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000823 //
824 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000825 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000826 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000827 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000828 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000829 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000830 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000831 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000832 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000833 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000834
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000835 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000836 case AsmToken::EqualEqual:
837 Kind = MCBinaryExpr::EQ;
838 return 3;
839 case AsmToken::ExclaimEqual:
840 case AsmToken::LessGreater:
841 Kind = MCBinaryExpr::NE;
842 return 3;
843 case AsmToken::Less:
844 Kind = MCBinaryExpr::LT;
845 return 3;
846 case AsmToken::LessEqual:
847 Kind = MCBinaryExpr::LTE;
848 return 3;
849 case AsmToken::Greater:
850 Kind = MCBinaryExpr::GT;
851 return 3;
852 case AsmToken::GreaterEqual:
853 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000854 return 3;
855
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000856 // High Intermediate Precedence: +, -
857 case AsmToken::Plus:
858 Kind = MCBinaryExpr::Add;
859 return 4;
860 case AsmToken::Minus:
861 Kind = MCBinaryExpr::Sub;
862 return 4;
863
Daniel Dunbar475839e2009-06-29 20:37:27 +0000864 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000865 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000866 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000867 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000868 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000869 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000870 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000871 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000872 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000873 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000874 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000875 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000876 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000877 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000878 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000879 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000880 }
881}
882
883
884/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
885/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000886bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
887 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000888 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000889 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000890 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000891
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000892 // If the next token is lower precedence than we are allowed to eat, return
893 // successfully with what we ate already.
894 if (TokPrec < Precedence)
895 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000896
Sean Callanan79ed1a82010-01-19 20:22:31 +0000897 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000898
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000899 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000900 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000901 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000902
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000903 // If BinOp binds less tightly with RHS than the operator after RHS, let
904 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000905 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000906 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000907 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000908 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000909 }
910
Daniel Dunbar475839e2009-06-29 20:37:27 +0000911 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000912 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000913 }
914}
915
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000916
917
918
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000919/// ParseStatement:
920/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000921/// ::= Label* Directive ...Operands... EndOfStatement
922/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000923bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000924 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000925 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000926 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000927 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000928 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000929
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000930 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000931 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000932 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000933 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000934 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000935 // A full line comment is a '#' as the first token.
936 if (Lexer.is(AsmToken::Hash)) {
937 EatToEndOfStatement();
938 return false;
939 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000940
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000941 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000942 if (Lexer.is(AsmToken::Integer)) {
943 LocalLabelVal = getTok().getIntVal();
944 if (LocalLabelVal < 0) {
945 if (!TheCondState.Ignore)
946 return TokError("unexpected token at start of statement");
947 IDVal = "";
948 }
949 else {
950 IDVal = getTok().getString();
951 Lex(); // Consume the integer token to be used as an identifier token.
952 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000953 if (!TheCondState.Ignore)
954 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000955 }
956 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000957
958 } else if (Lexer.is(AsmToken::Dot)) {
959 // Treat '.' as a valid identifier in this context.
960 Lex();
961 IDVal = ".";
962
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000963 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000964 if (!TheCondState.Ignore)
965 return TokError("unexpected token at start of statement");
966 IDVal = "";
967 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000968
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000969
Chris Lattner7834fac2010-04-17 18:14:27 +0000970 // Handle conditional assembly here before checking for skipping. We
971 // have to do this so that .endif isn't skipped in a ".if 0" block for
972 // example.
973 if (IDVal == ".if")
974 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000975 if (IDVal == ".ifdef")
976 return ParseDirectiveIfdef(IDLoc, true);
977 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
978 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000979 if (IDVal == ".elseif")
980 return ParseDirectiveElseIf(IDLoc);
981 if (IDVal == ".else")
982 return ParseDirectiveElse(IDLoc);
983 if (IDVal == ".endif")
984 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000985
Chris Lattner7834fac2010-04-17 18:14:27 +0000986 // If we are in a ".if 0" block, ignore this statement.
987 if (TheCondState.Ignore) {
988 EatToEndOfStatement();
989 return false;
990 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000991
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000992 // FIXME: Recurse on local labels?
993
994 // See what kind of statement we have.
995 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000996 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000997 CheckForValidSection();
998
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000999 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001000 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001001
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001002 // Diagnose attempt to use '.' as a label.
1003 if (IDVal == ".")
1004 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1005
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001006 // Diagnose attempt to use a variable as a label.
1007 //
1008 // FIXME: Diagnostics. Note the location of the definition as a label.
1009 // FIXME: This doesn't diagnose assignment to a symbol which has been
1010 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001011 MCSymbol *Sym;
1012 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001013 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001014 else
1015 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001016 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001017 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001018
Daniel Dunbar959fd882009-08-26 22:13:22 +00001019 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001020 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001021
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001022 // Consume any end of statement token, if present, to avoid spurious
1023 // AddBlankLine calls().
1024 if (Lexer.is(AsmToken::EndOfStatement)) {
1025 Lex();
1026 if (Lexer.is(AsmToken::Eof))
1027 return false;
1028 }
1029
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001030 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001031 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001032
Daniel Dunbar3f872332009-07-28 16:08:33 +00001033 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001034 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001035 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001036
Nico Weber4c4c7322011-01-28 03:04:41 +00001037 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001038
1039 default: // Normal instruction or directive.
1040 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001041 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001042
1043 // If macros are enabled, check to see if this is a macro instantiation.
1044 if (MacrosEnabled)
1045 if (const Macro *M = MacroMap.lookup(IDVal))
1046 return HandleMacroEntry(IDVal, IDLoc, M);
1047
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001048 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001049 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001050 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001051 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001052 return ParseDirectiveSet(IDVal, true);
1053 if (IDVal == ".equiv")
1054 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001055
Daniel Dunbara0d14262009-06-24 23:30:00 +00001056 // Data directives
1057
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001058 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001059 return ParseDirectiveAscii(IDVal, false);
1060 if (IDVal == ".asciz" || IDVal == ".string")
1061 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001062
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001063 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001064 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001065 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001066 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001067 if (IDVal == ".value")
1068 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001069 if (IDVal == ".2byte")
1070 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001071 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001072 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001073 if (IDVal == ".int")
1074 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001075 if (IDVal == ".4byte")
1076 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001077 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001078 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001079 if (IDVal == ".8byte")
1080 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001081 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001082 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1083 if (IDVal == ".double")
1084 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001085
Eli Friedman5d68ec22010-07-19 04:17:25 +00001086 if (IDVal == ".align") {
1087 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1088 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1089 }
1090 if (IDVal == ".align32") {
1091 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1092 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1093 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001094 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001095 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001096 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001097 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001098 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001099 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001100 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001101 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001102 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001103 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001104 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001105 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1106
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001107 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001108 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001109
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001110 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001111 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001112 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001113 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001114 if (IDVal == ".zero")
1115 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001116
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001117 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001118
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001119 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001120 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001121 // ELF only? Should it be here?
1122 if (IDVal == ".local")
1123 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001125 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001126 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001127 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001128 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001129 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001130 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001131 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001132 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001133 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001134 if (IDVal == ".symbol_resolver")
1135 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001136 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001137 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001138 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001139 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001140 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001141 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001142 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001143 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001144 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001145 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001146 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001147 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001148 if (IDVal == ".weak_def_can_be_hidden")
1149 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001150
Hans Wennborg5cc64912011-06-18 13:51:54 +00001151 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001152 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001153 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001154 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001155
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001156 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001157 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001158 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001159 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001160
Roman Divackybb6d14f2011-01-31 21:19:43 +00001161 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001162 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001163
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001164 // Look up the handler in the handler table.
1165 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1166 DirectiveMap.lookup(IDVal);
1167 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001168 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001169
Kevin Enderby9c656452009-09-10 20:51:44 +00001170 // Target hook for parsing target specific directives.
1171 if (!getTargetParser().ParseDirective(ID))
1172 return false;
1173
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001174 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001175 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001176 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001177 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001178
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001179 CheckForValidSection();
1180
Chris Lattnera7f13542010-05-19 23:34:33 +00001181 // Canonicalize the opcode to lower case.
1182 SmallString<128> Opcode;
1183 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1184 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001185
Chris Lattner98986712010-01-14 22:21:20 +00001186 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001187 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001188 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001189
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001190 // Dump the parsed representation, if requested.
1191 if (getShowParsedOperands()) {
1192 SmallString<256> Str;
1193 raw_svector_ostream OS(Str);
1194 OS << "parsed instruction: [";
1195 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1196 if (i != 0)
1197 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001198 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001199 }
1200 OS << "]";
1201
1202 PrintMessage(IDLoc, OS.str(), "note");
1203 }
1204
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001205 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001206 if (!HadError)
1207 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1208 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001209
Chris Lattner98986712010-01-14 22:21:20 +00001210 // Free any parsed operands.
1211 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1212 delete ParsedOperands[i];
1213
Chris Lattnercbf8a982010-09-11 16:18:25 +00001214 // Don't skip the rest of the line, the instruction parser is responsible for
1215 // that.
1216 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001217}
Chris Lattner9a023f72009-06-24 04:43:34 +00001218
Rafael Espindola65366442011-06-05 02:43:45 +00001219bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1220 const std::vector<StringRef> &Parameters,
1221 const std::vector<std::vector<AsmToken> > &A,
1222 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001223 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001224 unsigned NParameters = Parameters.size();
1225 if (NParameters != 0 && NParameters != A.size())
1226 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001227
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001228 while (!Body.empty()) {
1229 // Scan for the next substitution.
1230 std::size_t End = Body.size(), Pos = 0;
1231 for (; Pos != End; ++Pos) {
1232 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001233 if (!NParameters) {
1234 // This macro has no parameters, look for $0, $1, etc.
1235 if (Body[Pos] != '$' || Pos + 1 == End)
1236 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001237
Rafael Espindola65366442011-06-05 02:43:45 +00001238 char Next = Body[Pos + 1];
1239 if (Next == '$' || Next == 'n' || isdigit(Next))
1240 break;
1241 } else {
1242 // This macro has parameters, look for \foo, \bar, etc.
1243 if (Body[Pos] == '\\' && Pos + 1 != End)
1244 break;
1245 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001246 }
1247
1248 // Add the prefix.
1249 OS << Body.slice(0, Pos);
1250
1251 // Check if we reached the end.
1252 if (Pos == End)
1253 break;
1254
Rafael Espindola65366442011-06-05 02:43:45 +00001255 if (!NParameters) {
1256 switch (Body[Pos+1]) {
1257 // $$ => $
1258 case '$':
1259 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001260 break;
1261
Rafael Espindola65366442011-06-05 02:43:45 +00001262 // $n => number of arguments
1263 case 'n':
1264 OS << A.size();
1265 break;
1266
1267 // $[0-9] => argument
1268 default: {
1269 // Missing arguments are ignored.
1270 unsigned Index = Body[Pos+1] - '0';
1271 if (Index >= A.size())
1272 break;
1273
1274 // Otherwise substitute with the token values, with spaces eliminated.
1275 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1276 ie = A[Index].end(); it != ie; ++it)
1277 OS << it->getString();
1278 break;
1279 }
1280 }
1281 Pos += 2;
1282 } else {
1283 unsigned I = Pos + 1;
1284 while (isalnum(Body[I]) && I + 1 != End)
1285 ++I;
1286
1287 const char *Begin = Body.data() + Pos +1;
1288 StringRef Argument(Begin, I - (Pos +1));
1289 unsigned Index = 0;
1290 for (; Index < NParameters; ++Index)
1291 if (Parameters[Index] == Argument)
1292 break;
1293
1294 // FIXME: We should error at the macro definition.
1295 if (Index == NParameters)
1296 return Error(L, "Parameter not found");
1297
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001298 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1299 ie = A[Index].end(); it != ie; ++it)
1300 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001301
Rafael Espindola65366442011-06-05 02:43:45 +00001302 Pos += 1 + Argument.size();
1303 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001304 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001305 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001306 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001307
1308 // We include the .endmacro in the buffer as our queue to exit the macro
1309 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001310 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001311 return false;
1312}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001313
Rafael Espindola65366442011-06-05 02:43:45 +00001314MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1315 MemoryBuffer *I)
1316 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1317{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001318}
1319
1320bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1321 const Macro *M) {
1322 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1323 // this, although we should protect against infinite loops.
1324 if (ActiveMacros.size() == 20)
1325 return TokError("macros cannot be nested more than 20 levels deep");
1326
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001327 // Parse the macro instantiation arguments.
1328 std::vector<std::vector<AsmToken> > MacroArguments;
1329 MacroArguments.push_back(std::vector<AsmToken>());
1330 unsigned ParenLevel = 0;
1331 for (;;) {
1332 if (Lexer.is(AsmToken::Eof))
1333 return TokError("unexpected token in macro instantiation");
1334 if (Lexer.is(AsmToken::EndOfStatement))
1335 break;
1336
1337 // If we aren't inside parentheses and this is a comma, start a new token
1338 // list.
1339 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1340 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001341 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001342 // Adjust the current parentheses level.
1343 if (Lexer.is(AsmToken::LParen))
1344 ++ParenLevel;
1345 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1346 --ParenLevel;
1347
1348 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001349 MacroArguments.back().push_back(getTok());
1350 }
1351 Lex();
1352 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001353
Rafael Espindola65366442011-06-05 02:43:45 +00001354 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1355 // to hold the macro body with substitutions.
1356 SmallString<256> Buf;
1357 StringRef Body = M->Body;
1358
1359 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1360 return true;
1361
1362 MemoryBuffer *Instantiation =
1363 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1364
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001365 // Create the macro instantiation object and add to the current macro
1366 // instantiation stack.
1367 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001368 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001369 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001370 ActiveMacros.push_back(MI);
1371
1372 // Jump to the macro instantiation and prime the lexer.
1373 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1374 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1375 Lex();
1376
1377 return false;
1378}
1379
1380void AsmParser::HandleMacroExit() {
1381 // Jump to the EndOfStatement we should return to, and consume it.
1382 JumpToLoc(ActiveMacros.back()->ExitLoc);
1383 Lex();
1384
1385 // Pop the instantiation entry.
1386 delete ActiveMacros.back();
1387 ActiveMacros.pop_back();
1388}
1389
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001390static void MarkUsed(const MCExpr *Value) {
1391 switch (Value->getKind()) {
1392 case MCExpr::Binary:
1393 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1394 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1395 break;
1396 case MCExpr::Target:
1397 case MCExpr::Constant:
1398 break;
1399 case MCExpr::SymbolRef: {
1400 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1401 break;
1402 }
1403 case MCExpr::Unary:
1404 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1405 break;
1406 }
1407}
1408
Nico Weber4c4c7322011-01-28 03:04:41 +00001409bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001410 // FIXME: Use better location, we should use proper tokens.
1411 SMLoc EqualLoc = Lexer.getLoc();
1412
Daniel Dunbar821e3332009-08-31 08:09:28 +00001413 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001414 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001415 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001416
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001417 MarkUsed(Value);
1418
Daniel Dunbar3f872332009-07-28 16:08:33 +00001419 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001420 return TokError("unexpected token in assignment");
1421
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001422 // Error on assignment to '.'.
1423 if (Name == ".") {
1424 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1425 "(use '.space' or '.org').)"));
1426 }
1427
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001428 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001429 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001430
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001431 // Validate that the LHS is allowed to be a variable (either it has not been
1432 // used as a symbol, or it is an absolute symbol).
1433 MCSymbol *Sym = getContext().LookupSymbol(Name);
1434 if (Sym) {
1435 // Diagnose assignment to a label.
1436 //
1437 // FIXME: Diagnostics. Note the location of the definition as a label.
1438 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001439 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001440 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001441 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001442 return Error(EqualLoc, "redefinition of '" + Name + "'");
1443 else if (!Sym->isVariable())
1444 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001445 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001446 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1447 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001448
1449 // Don't count these checks as uses.
1450 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001451 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001452 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001453
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001454 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001455
1456 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001457 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001458
1459 return false;
1460}
1461
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001462/// ParseIdentifier:
1463/// ::= identifier
1464/// ::= string
1465bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001466 // The assembler has relaxed rules for accepting identifiers, in particular we
1467 // allow things like '.globl $foo', which would normally be separate
1468 // tokens. At this level, we have already lexed so we cannot (currently)
1469 // handle this as a context dependent token, instead we detect adjacent tokens
1470 // and return the combined identifier.
1471 if (Lexer.is(AsmToken::Dollar)) {
1472 SMLoc DollarLoc = getLexer().getLoc();
1473
1474 // Consume the dollar sign, and check for a following identifier.
1475 Lex();
1476 if (Lexer.isNot(AsmToken::Identifier))
1477 return true;
1478
1479 // We have a '$' followed by an identifier, make sure they are adjacent.
1480 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1481 return true;
1482
1483 // Construct the joined identifier and consume the token.
1484 Res = StringRef(DollarLoc.getPointer(),
1485 getTok().getIdentifier().size() + 1);
1486 Lex();
1487 return false;
1488 }
1489
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001490 if (Lexer.isNot(AsmToken::Identifier) &&
1491 Lexer.isNot(AsmToken::String))
1492 return true;
1493
Sean Callanan18b83232010-01-19 21:44:56 +00001494 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001495
Sean Callanan79ed1a82010-01-19 20:22:31 +00001496 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001497
1498 return false;
1499}
1500
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001501/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001502/// ::= .equ identifier ',' expression
1503/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001504/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001505bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001506 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001507
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001508 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001509 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001510
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001511 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001512 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001513 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001514
Nico Weber4c4c7322011-01-28 03:04:41 +00001515 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001516}
1517
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001518bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001519 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001520
1521 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001522 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001523 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1524 if (Str[i] != '\\') {
1525 Data += Str[i];
1526 continue;
1527 }
1528
1529 // Recognize escaped characters. Note that this escape semantics currently
1530 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1531 ++i;
1532 if (i == e)
1533 return TokError("unexpected backslash at end of string");
1534
1535 // Recognize octal sequences.
1536 if ((unsigned) (Str[i] - '0') <= 7) {
1537 // Consume up to three octal characters.
1538 unsigned Value = Str[i] - '0';
1539
1540 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1541 ++i;
1542 Value = Value * 8 + (Str[i] - '0');
1543
1544 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1545 ++i;
1546 Value = Value * 8 + (Str[i] - '0');
1547 }
1548 }
1549
1550 if (Value > 255)
1551 return TokError("invalid octal escape sequence (out of range)");
1552
1553 Data += (unsigned char) Value;
1554 continue;
1555 }
1556
1557 // Otherwise recognize individual escapes.
1558 switch (Str[i]) {
1559 default:
1560 // Just reject invalid escape sequences for now.
1561 return TokError("invalid escape sequence (unrecognized character)");
1562
1563 case 'b': Data += '\b'; break;
1564 case 'f': Data += '\f'; break;
1565 case 'n': Data += '\n'; break;
1566 case 'r': Data += '\r'; break;
1567 case 't': Data += '\t'; break;
1568 case '"': Data += '"'; break;
1569 case '\\': Data += '\\'; break;
1570 }
1571 }
1572
1573 return false;
1574}
1575
Daniel Dunbara0d14262009-06-24 23:30:00 +00001576/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001577/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1578bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001579 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001580 CheckForValidSection();
1581
Daniel Dunbara0d14262009-06-24 23:30:00 +00001582 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001583 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001584 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001585
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001586 std::string Data;
1587 if (ParseEscapedString(Data))
1588 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001589
1590 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001591 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001592 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1593
Sean Callanan79ed1a82010-01-19 20:22:31 +00001594 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001595
1596 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001597 break;
1598
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001599 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001600 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001601 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001602 }
1603 }
1604
Sean Callanan79ed1a82010-01-19 20:22:31 +00001605 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001606 return false;
1607}
1608
1609/// ParseDirectiveValue
1610/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1611bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001612 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001613 CheckForValidSection();
1614
Daniel Dunbara0d14262009-06-24 23:30:00 +00001615 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001616 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001617 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001618 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001619 return true;
1620
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001621 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001622 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1623 assert(Size <= 8 && "Invalid size");
1624 uint64_t IntValue = MCE->getValue();
1625 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1626 return Error(ExprLoc, "literal value out of range for directive");
1627 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1628 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001629 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001630
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001631 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001632 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001633
Daniel Dunbara0d14262009-06-24 23:30:00 +00001634 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001635 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001636 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001637 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001638 }
1639 }
1640
Sean Callanan79ed1a82010-01-19 20:22:31 +00001641 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001642 return false;
1643}
1644
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001645/// ParseDirectiveRealValue
1646/// ::= (.single | .double) [ expression (, expression)* ]
1647bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1648 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1649 CheckForValidSection();
1650
1651 for (;;) {
1652 // We don't truly support arithmetic on floating point expressions, so we
1653 // have to manually parse unary prefixes.
1654 bool IsNeg = false;
1655 if (getLexer().is(AsmToken::Minus)) {
1656 Lex();
1657 IsNeg = true;
1658 } else if (getLexer().is(AsmToken::Plus))
1659 Lex();
1660
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001661 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001662 getLexer().isNot(AsmToken::Real) &&
1663 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001664 return TokError("unexpected token in directive");
1665
1666 // Convert to an APFloat.
1667 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001668 StringRef IDVal = getTok().getString();
1669 if (getLexer().is(AsmToken::Identifier)) {
1670 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1671 Value = APFloat::getInf(Semantics);
1672 else if (!IDVal.compare_lower("nan"))
1673 Value = APFloat::getNaN(Semantics, false, ~0);
1674 else
1675 return TokError("invalid floating point literal");
1676 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001677 APFloat::opInvalidOp)
1678 return TokError("invalid floating point literal");
1679 if (IsNeg)
1680 Value.changeSign();
1681
1682 // Consume the numeric token.
1683 Lex();
1684
1685 // Emit the value as an integer.
1686 APInt AsInt = Value.bitcastToAPInt();
1687 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1688 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1689
1690 if (getLexer().is(AsmToken::EndOfStatement))
1691 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001692
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001693 if (getLexer().isNot(AsmToken::Comma))
1694 return TokError("unexpected token in directive");
1695 Lex();
1696 }
1697 }
1698
1699 Lex();
1700 return false;
1701}
1702
Daniel Dunbara0d14262009-06-24 23:30:00 +00001703/// ParseDirectiveSpace
1704/// ::= .space expression [ , expression ]
1705bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001706 CheckForValidSection();
1707
Daniel Dunbara0d14262009-06-24 23:30:00 +00001708 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001709 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001710 return true;
1711
1712 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001713 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1714 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001715 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001716 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001717
Daniel Dunbar475839e2009-06-29 20:37:27 +00001718 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001719 return true;
1720
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001721 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001722 return TokError("unexpected token in '.space' directive");
1723 }
1724
Sean Callanan79ed1a82010-01-19 20:22:31 +00001725 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001726
1727 if (NumBytes <= 0)
1728 return TokError("invalid number of bytes in '.space' directive");
1729
1730 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001731 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001732
1733 return false;
1734}
1735
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001736/// ParseDirectiveZero
1737/// ::= .zero expression
1738bool AsmParser::ParseDirectiveZero() {
1739 CheckForValidSection();
1740
1741 int64_t NumBytes;
1742 if (ParseAbsoluteExpression(NumBytes))
1743 return true;
1744
Rafael Espindolae452b172010-10-05 19:42:57 +00001745 int64_t Val = 0;
1746 if (getLexer().is(AsmToken::Comma)) {
1747 Lex();
1748 if (ParseAbsoluteExpression(Val))
1749 return true;
1750 }
1751
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001752 if (getLexer().isNot(AsmToken::EndOfStatement))
1753 return TokError("unexpected token in '.zero' directive");
1754
1755 Lex();
1756
Rafael Espindolae452b172010-10-05 19:42:57 +00001757 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001758
1759 return false;
1760}
1761
Daniel Dunbara0d14262009-06-24 23:30:00 +00001762/// ParseDirectiveFill
1763/// ::= .fill expression , expression , expression
1764bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001765 CheckForValidSection();
1766
Daniel Dunbara0d14262009-06-24 23:30:00 +00001767 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001768 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001769 return true;
1770
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001771 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001772 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001773 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001774
Daniel Dunbara0d14262009-06-24 23:30:00 +00001775 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001776 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001777 return true;
1778
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001779 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001780 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001781 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001782
Daniel Dunbara0d14262009-06-24 23:30:00 +00001783 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001784 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001785 return true;
1786
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001787 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001788 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001789
Sean Callanan79ed1a82010-01-19 20:22:31 +00001790 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001791
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001792 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1793 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001794
1795 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001796 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001797
1798 return false;
1799}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001800
1801/// ParseDirectiveOrg
1802/// ::= .org expression [ , expression ]
1803bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001804 CheckForValidSection();
1805
Daniel Dunbar821e3332009-08-31 08:09:28 +00001806 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001807 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001808 return true;
1809
1810 // Parse optional fill expression.
1811 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001812 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1813 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001814 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001815 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001816
Daniel Dunbar475839e2009-06-29 20:37:27 +00001817 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001818 return true;
1819
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001820 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001821 return TokError("unexpected token in '.org' directive");
1822 }
1823
Sean Callanan79ed1a82010-01-19 20:22:31 +00001824 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001825
1826 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1827 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001828 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001829
1830 return false;
1831}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001832
1833/// ParseDirectiveAlign
1834/// ::= {.align, ...} expression [ , expression [ , expression ]]
1835bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001836 CheckForValidSection();
1837
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001838 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001839 int64_t Alignment;
1840 if (ParseAbsoluteExpression(Alignment))
1841 return true;
1842
1843 SMLoc MaxBytesLoc;
1844 bool HasFillExpr = false;
1845 int64_t FillExpr = 0;
1846 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001847 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1848 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001849 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001850 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001851
1852 // The fill expression can be omitted while specifying a maximum number of
1853 // alignment bytes, e.g:
1854 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001855 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001856 HasFillExpr = true;
1857 if (ParseAbsoluteExpression(FillExpr))
1858 return true;
1859 }
1860
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001861 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1862 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001863 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001864 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001865
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001866 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001867 if (ParseAbsoluteExpression(MaxBytesToFill))
1868 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001869
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001870 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001871 return TokError("unexpected token in directive");
1872 }
1873 }
1874
Sean Callanan79ed1a82010-01-19 20:22:31 +00001875 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001876
Daniel Dunbar648ac512010-05-17 21:54:30 +00001877 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001878 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001879
1880 // Compute alignment in bytes.
1881 if (IsPow2) {
1882 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001883 if (Alignment >= 32) {
1884 Error(AlignmentLoc, "invalid alignment value");
1885 Alignment = 31;
1886 }
1887
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001888 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001889 }
1890
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001891 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001892 if (MaxBytesLoc.isValid()) {
1893 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001894 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1895 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001896 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001897 }
1898
1899 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001900 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1901 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001902 MaxBytesToFill = 0;
1903 }
1904 }
1905
Daniel Dunbar648ac512010-05-17 21:54:30 +00001906 // Check whether we should use optimal code alignment for this .align
1907 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001908 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001909 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1910 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001911 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001912 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001913 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001914 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1915 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001916 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001917
1918 return false;
1919}
1920
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001921/// ParseDirectiveSymbolAttribute
1922/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001923bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001924 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001925 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001926 StringRef Name;
1927
1928 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001929 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001930
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001931 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001932
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001933 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001934
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001935 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001936 break;
1937
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001938 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001939 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001940 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001941 }
1942 }
1943
Sean Callanan79ed1a82010-01-19 20:22:31 +00001944 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001945 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001946}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001947
1948/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001949/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1950bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001951 CheckForValidSection();
1952
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001953 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001954 StringRef Name;
1955 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001956 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001957
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001958 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001959 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001960
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001961 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001962 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001963 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001964
1965 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001966 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001967 if (ParseAbsoluteExpression(Size))
1968 return true;
1969
1970 int64_t Pow2Alignment = 0;
1971 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001972 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001973 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001974 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001975 if (ParseAbsoluteExpression(Pow2Alignment))
1976 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001977
Chris Lattner258281d2010-01-19 06:22:22 +00001978 // If this target takes alignments in bytes (not log) validate and convert.
1979 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1980 if (!isPowerOf2_64(Pow2Alignment))
1981 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1982 Pow2Alignment = Log2_64(Pow2Alignment);
1983 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001984 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001985
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001986 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001987 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001988
Sean Callanan79ed1a82010-01-19 20:22:31 +00001989 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001990
Chris Lattner1fc3d752009-07-09 17:25:12 +00001991 // NOTE: a size of zero for a .comm should create a undefined symbol
1992 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001993 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001994 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1995 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001996
Eric Christopherc260a3e2010-05-14 01:38:54 +00001997 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001998 // may internally end up wanting an alignment in bytes.
1999 // FIXME: Diagnose overflow.
2000 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002001 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2002 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002003
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002004 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002005 return Error(IDLoc, "invalid symbol redefinition");
2006
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002007 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002008 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002009 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002010 getStreamer().EmitZerofill(Ctx.getMachOSection(
2011 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2012 0, SectionKind::getBSS()),
2013 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002014 return false;
2015 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002016
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002017 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002018 return false;
2019}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002020
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002021/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002022/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002023bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002024 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002025 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002026
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002027 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002028 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002029 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002030
Sean Callanan79ed1a82010-01-19 20:22:31 +00002031 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002032
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002033 if (Str.empty())
2034 Error(Loc, ".abort detected. Assembly stopping.");
2035 else
2036 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002037 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002038
2039 return false;
2040}
Kevin Enderby71148242009-07-14 21:35:03 +00002041
Kevin Enderby1f049b22009-07-14 23:21:55 +00002042/// ParseDirectiveInclude
2043/// ::= .include "filename"
2044bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002045 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002046 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002047
Sean Callanan18b83232010-01-19 21:44:56 +00002048 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002049 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002050 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002051
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002052 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002053 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002054
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002055 // Strip the quotes.
2056 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002057
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002058 // Attempt to switch the lexer to the included file before consuming the end
2059 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002060 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002061 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002062 return true;
2063 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002064
2065 return false;
2066}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002067
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002068/// ParseDirectiveIf
2069/// ::= .if expression
2070bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002071 TheCondStack.push_back(TheCondState);
2072 TheCondState.TheCond = AsmCond::IfCond;
2073 if(TheCondState.Ignore) {
2074 EatToEndOfStatement();
2075 }
2076 else {
2077 int64_t ExprValue;
2078 if (ParseAbsoluteExpression(ExprValue))
2079 return true;
2080
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002081 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002082 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002083
Sean Callanan79ed1a82010-01-19 20:22:31 +00002084 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002085
2086 TheCondState.CondMet = ExprValue;
2087 TheCondState.Ignore = !TheCondState.CondMet;
2088 }
2089
2090 return false;
2091}
2092
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002093bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2094 StringRef Name;
2095 TheCondStack.push_back(TheCondState);
2096 TheCondState.TheCond = AsmCond::IfCond;
2097
2098 if (TheCondState.Ignore) {
2099 EatToEndOfStatement();
2100 } else {
2101 if (ParseIdentifier(Name))
2102 return TokError("expected identifier after '.ifdef'");
2103
2104 Lex();
2105
2106 MCSymbol *Sym = getContext().LookupSymbol(Name);
2107
2108 if (expect_defined)
2109 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2110 else
2111 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2112 TheCondState.Ignore = !TheCondState.CondMet;
2113 }
2114
2115 return false;
2116}
2117
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002118/// ParseDirectiveElseIf
2119/// ::= .elseif expression
2120bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2121 if (TheCondState.TheCond != AsmCond::IfCond &&
2122 TheCondState.TheCond != AsmCond::ElseIfCond)
2123 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2124 " an .elseif");
2125 TheCondState.TheCond = AsmCond::ElseIfCond;
2126
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002127 bool LastIgnoreState = false;
2128 if (!TheCondStack.empty())
2129 LastIgnoreState = TheCondStack.back().Ignore;
2130 if (LastIgnoreState || TheCondState.CondMet) {
2131 TheCondState.Ignore = true;
2132 EatToEndOfStatement();
2133 }
2134 else {
2135 int64_t ExprValue;
2136 if (ParseAbsoluteExpression(ExprValue))
2137 return true;
2138
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002139 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002140 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002141
Sean Callanan79ed1a82010-01-19 20:22:31 +00002142 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002143 TheCondState.CondMet = ExprValue;
2144 TheCondState.Ignore = !TheCondState.CondMet;
2145 }
2146
2147 return false;
2148}
2149
2150/// ParseDirectiveElse
2151/// ::= .else
2152bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002153 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002154 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002155
Sean Callanan79ed1a82010-01-19 20:22:31 +00002156 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002157
2158 if (TheCondState.TheCond != AsmCond::IfCond &&
2159 TheCondState.TheCond != AsmCond::ElseIfCond)
2160 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2161 ".elseif");
2162 TheCondState.TheCond = AsmCond::ElseCond;
2163 bool LastIgnoreState = false;
2164 if (!TheCondStack.empty())
2165 LastIgnoreState = TheCondStack.back().Ignore;
2166 if (LastIgnoreState || TheCondState.CondMet)
2167 TheCondState.Ignore = true;
2168 else
2169 TheCondState.Ignore = false;
2170
2171 return false;
2172}
2173
2174/// ParseDirectiveEndIf
2175/// ::= .endif
2176bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002177 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002178 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002179
Sean Callanan79ed1a82010-01-19 20:22:31 +00002180 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002181
2182 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2183 TheCondStack.empty())
2184 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2185 ".else");
2186 if (!TheCondStack.empty()) {
2187 TheCondState = TheCondStack.back();
2188 TheCondStack.pop_back();
2189 }
2190
2191 return false;
2192}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002193
2194/// ParseDirectiveFile
2195/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002196bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002197 // FIXME: I'm not sure what this is.
2198 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002199 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002200 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002201 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002202 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002203
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002204 if (FileNumber < 1)
2205 return TokError("file number less than one");
2206 }
2207
Daniel Dunbareceec052010-07-12 17:45:27 +00002208 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002209 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002210
Chris Lattnerd32e8032010-01-25 19:02:58 +00002211 StringRef Filename = getTok().getString();
2212 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002213 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002214
Daniel Dunbareceec052010-07-12 17:45:27 +00002215 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002216 return TokError("unexpected token in '.file' directive");
2217
Chris Lattnerd32e8032010-01-25 19:02:58 +00002218 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002219 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002220 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002221 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002222 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002223 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002224
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002225 return false;
2226}
2227
2228/// ParseDirectiveLine
2229/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002230bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002231 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2232 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002233 return TokError("unexpected token in '.line' directive");
2234
Sean Callanan18b83232010-01-19 21:44:56 +00002235 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002236 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002237 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002238
2239 // FIXME: Do something with the .line.
2240 }
2241
Daniel Dunbareceec052010-07-12 17:45:27 +00002242 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002243 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002244
2245 return false;
2246}
2247
2248
2249/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002250/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002251/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2252/// The first number is a file number, must have been previously assigned with
2253/// a .file directive, the second number is the line number and optionally the
2254/// third number is a column position (zero if not specified). The remaining
2255/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002256bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002257
Daniel Dunbareceec052010-07-12 17:45:27 +00002258 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002259 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002260 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002261 if (FileNumber < 1)
2262 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002263 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002264 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002265 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002266
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002267 int64_t LineNumber = 0;
2268 if (getLexer().is(AsmToken::Integer)) {
2269 LineNumber = getTok().getIntVal();
2270 if (LineNumber < 1)
2271 return TokError("line number less than one in '.loc' directive");
2272 Lex();
2273 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002274
2275 int64_t ColumnPos = 0;
2276 if (getLexer().is(AsmToken::Integer)) {
2277 ColumnPos = getTok().getIntVal();
2278 if (ColumnPos < 0)
2279 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002280 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002281 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002282
Kevin Enderbyc0957932010-09-30 16:52:03 +00002283 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002284 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002285 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002286 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2287 for (;;) {
2288 if (getLexer().is(AsmToken::EndOfStatement))
2289 break;
2290
2291 StringRef Name;
2292 SMLoc Loc = getTok().getLoc();
2293 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002294 return TokError("unexpected token in '.loc' directive");
2295
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002296 if (Name == "basic_block")
2297 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2298 else if (Name == "prologue_end")
2299 Flags |= DWARF2_FLAG_PROLOGUE_END;
2300 else if (Name == "epilogue_begin")
2301 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2302 else if (Name == "is_stmt") {
2303 SMLoc Loc = getTok().getLoc();
2304 const MCExpr *Value;
2305 if (getParser().ParseExpression(Value))
2306 return true;
2307 // The expression must be the constant 0 or 1.
2308 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2309 int Value = MCE->getValue();
2310 if (Value == 0)
2311 Flags &= ~DWARF2_FLAG_IS_STMT;
2312 else if (Value == 1)
2313 Flags |= DWARF2_FLAG_IS_STMT;
2314 else
2315 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002316 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002317 else {
2318 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2319 }
2320 }
2321 else if (Name == "isa") {
2322 SMLoc Loc = getTok().getLoc();
2323 const MCExpr *Value;
2324 if (getParser().ParseExpression(Value))
2325 return true;
2326 // The expression must be a constant greater or equal to 0.
2327 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2328 int Value = MCE->getValue();
2329 if (Value < 0)
2330 return Error(Loc, "isa number less than zero");
2331 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002332 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002333 else {
2334 return Error(Loc, "isa number not a constant value");
2335 }
2336 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002337 else if (Name == "discriminator") {
2338 if (getParser().ParseAbsoluteExpression(Discriminator))
2339 return true;
2340 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002341 else {
2342 return Error(Loc, "unknown sub-directive in '.loc' directive");
2343 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002344
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002345 if (getLexer().is(AsmToken::EndOfStatement))
2346 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002347 }
2348 }
2349
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002350 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002351 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002352
2353 return false;
2354}
2355
Daniel Dunbar138abae2010-10-16 04:56:42 +00002356/// ParseDirectiveStabs
2357/// ::= .stabs string, number, number, number
2358bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2359 SMLoc DirectiveLoc) {
2360 return TokError("unsupported directive '" + Directive + "'");
2361}
2362
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002363/// ParseDirectiveCFISections
2364/// ::= .cfi_sections section [, section]
2365bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2366 SMLoc DirectiveLoc) {
2367 StringRef Name;
2368 bool EH = false;
2369 bool Debug = false;
2370
2371 if (getParser().ParseIdentifier(Name))
2372 return TokError("Expected an identifier");
2373
2374 if (Name == ".eh_frame")
2375 EH = true;
2376 else if (Name == ".debug_frame")
2377 Debug = true;
2378
2379 if (getLexer().is(AsmToken::Comma)) {
2380 Lex();
2381
2382 if (getParser().ParseIdentifier(Name))
2383 return TokError("Expected an identifier");
2384
2385 if (Name == ".eh_frame")
2386 EH = true;
2387 else if (Name == ".debug_frame")
2388 Debug = true;
2389 }
2390
2391 getStreamer().EmitCFISections(EH, Debug);
2392
2393 return false;
2394}
2395
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002396/// ParseDirectiveCFIStartProc
2397/// ::= .cfi_startproc
2398bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2399 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002400 getStreamer().EmitCFIStartProc();
2401 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002402}
2403
2404/// ParseDirectiveCFIEndProc
2405/// ::= .cfi_endproc
2406bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002407 getStreamer().EmitCFIEndProc();
2408 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002409}
2410
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002411/// ParseRegisterOrRegisterNumber - parse register name or number.
2412bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2413 SMLoc DirectiveLoc) {
2414 unsigned RegNo;
2415
Jim Grosbach6f888a82011-06-02 17:14:04 +00002416 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002417 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2418 DirectiveLoc))
2419 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002420 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002421 } else
2422 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002423
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002424 return false;
2425}
2426
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002427/// ParseDirectiveCFIDefCfa
2428/// ::= .cfi_def_cfa register, offset
2429bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2430 SMLoc DirectiveLoc) {
2431 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002432 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002433 return true;
2434
2435 if (getLexer().isNot(AsmToken::Comma))
2436 return TokError("unexpected token in directive");
2437 Lex();
2438
2439 int64_t Offset = 0;
2440 if (getParser().ParseAbsoluteExpression(Offset))
2441 return true;
2442
Rafael Espindola066c2f42011-04-12 23:59:07 +00002443 getStreamer().EmitCFIDefCfa(Register, Offset);
2444 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002445}
2446
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002447/// ParseDirectiveCFIDefCfaOffset
2448/// ::= .cfi_def_cfa_offset offset
2449bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2450 SMLoc DirectiveLoc) {
2451 int64_t Offset = 0;
2452 if (getParser().ParseAbsoluteExpression(Offset))
2453 return true;
2454
Rafael Espindola066c2f42011-04-12 23:59:07 +00002455 getStreamer().EmitCFIDefCfaOffset(Offset);
2456 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002457}
2458
2459/// ParseDirectiveCFIAdjustCfaOffset
2460/// ::= .cfi_adjust_cfa_offset adjustment
2461bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2462 SMLoc DirectiveLoc) {
2463 int64_t Adjustment = 0;
2464 if (getParser().ParseAbsoluteExpression(Adjustment))
2465 return true;
2466
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002467 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2468 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002469}
2470
2471/// ParseDirectiveCFIDefCfaRegister
2472/// ::= .cfi_def_cfa_register register
2473bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2474 SMLoc DirectiveLoc) {
2475 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002476 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002477 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002478
Rafael Espindola066c2f42011-04-12 23:59:07 +00002479 getStreamer().EmitCFIDefCfaRegister(Register);
2480 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002481}
2482
2483/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002484/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002485bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2486 int64_t Register = 0;
2487 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002488
2489 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002490 return true;
2491
2492 if (getLexer().isNot(AsmToken::Comma))
2493 return TokError("unexpected token in directive");
2494 Lex();
2495
2496 if (getParser().ParseAbsoluteExpression(Offset))
2497 return true;
2498
Rafael Espindola066c2f42011-04-12 23:59:07 +00002499 getStreamer().EmitCFIOffset(Register, Offset);
2500 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002501}
2502
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002503/// ParseDirectiveCFIRelOffset
2504/// ::= .cfi_rel_offset register, offset
2505bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2506 SMLoc DirectiveLoc) {
2507 int64_t Register = 0;
2508
2509 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2510 return true;
2511
2512 if (getLexer().isNot(AsmToken::Comma))
2513 return TokError("unexpected token in directive");
2514 Lex();
2515
2516 int64_t Offset = 0;
2517 if (getParser().ParseAbsoluteExpression(Offset))
2518 return true;
2519
Rafael Espindola25f492e2011-04-12 16:12:03 +00002520 getStreamer().EmitCFIRelOffset(Register, Offset);
2521 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002522}
2523
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002524static bool isValidEncoding(int64_t Encoding) {
2525 if (Encoding & ~0xff)
2526 return false;
2527
2528 if (Encoding == dwarf::DW_EH_PE_omit)
2529 return true;
2530
2531 const unsigned Format = Encoding & 0xf;
2532 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2533 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2534 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2535 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2536 return false;
2537
Rafael Espindolacaf11582010-12-29 04:31:26 +00002538 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002539 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002540 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002541 return false;
2542
2543 return true;
2544}
2545
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002546/// ParseDirectiveCFIPersonalityOrLsda
2547/// ::= .cfi_personality encoding, [symbol_name]
2548/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002549bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002550 SMLoc DirectiveLoc) {
2551 int64_t Encoding = 0;
2552 if (getParser().ParseAbsoluteExpression(Encoding))
2553 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002554 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002555 return false;
2556
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002557 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002558 return TokError("unsupported encoding.");
2559
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002560 if (getLexer().isNot(AsmToken::Comma))
2561 return TokError("unexpected token in directive");
2562 Lex();
2563
2564 StringRef Name;
2565 if (getParser().ParseIdentifier(Name))
2566 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002567
2568 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2569
2570 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002571 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002572 else {
2573 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002574 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002575 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002576 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002577}
2578
Rafael Espindolafe024d02010-12-28 18:36:23 +00002579/// ParseDirectiveCFIRememberState
2580/// ::= .cfi_remember_state
2581bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2582 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002583 getStreamer().EmitCFIRememberState();
2584 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002585}
2586
2587/// ParseDirectiveCFIRestoreState
2588/// ::= .cfi_remember_state
2589bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2590 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002591 getStreamer().EmitCFIRestoreState();
2592 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002593}
2594
Rafael Espindolac5754392011-04-12 15:31:05 +00002595/// ParseDirectiveCFISameValue
2596/// ::= .cfi_same_value register
2597bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2598 SMLoc DirectiveLoc) {
2599 int64_t Register = 0;
2600
2601 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2602 return true;
2603
2604 getStreamer().EmitCFISameValue(Register);
2605
2606 return false;
2607}
2608
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002609/// ParseDirectiveMacrosOnOff
2610/// ::= .macros_on
2611/// ::= .macros_off
2612bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2613 SMLoc DirectiveLoc) {
2614 if (getLexer().isNot(AsmToken::EndOfStatement))
2615 return Error(getLexer().getLoc(),
2616 "unexpected token in '" + Directive + "' directive");
2617
2618 getParser().MacrosEnabled = Directive == ".macros_on";
2619
2620 return false;
2621}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002622
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002623/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002624/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002625bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2626 SMLoc DirectiveLoc) {
2627 StringRef Name;
2628 if (getParser().ParseIdentifier(Name))
2629 return TokError("expected identifier in directive");
2630
Rafael Espindola65366442011-06-05 02:43:45 +00002631 std::vector<StringRef> Parameters;
2632 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2633 for(;;) {
2634 StringRef Parameter;
2635 if (getParser().ParseIdentifier(Parameter))
2636 return TokError("expected identifier in directive");
2637 Parameters.push_back(Parameter);
2638
2639 if (getLexer().isNot(AsmToken::Comma))
2640 break;
2641 Lex();
2642 }
2643 }
2644
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002645 if (getLexer().isNot(AsmToken::EndOfStatement))
2646 return TokError("unexpected token in '.macro' directive");
2647
2648 // Eat the end of statement.
2649 Lex();
2650
2651 AsmToken EndToken, StartToken = getTok();
2652
2653 // Lex the macro definition.
2654 for (;;) {
2655 // Check whether we have reached the end of the file.
2656 if (getLexer().is(AsmToken::Eof))
2657 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2658
2659 // Otherwise, check whether we have reach the .endmacro.
2660 if (getLexer().is(AsmToken::Identifier) &&
2661 (getTok().getIdentifier() == ".endm" ||
2662 getTok().getIdentifier() == ".endmacro")) {
2663 EndToken = getTok();
2664 Lex();
2665 if (getLexer().isNot(AsmToken::EndOfStatement))
2666 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2667 "' directive");
2668 break;
2669 }
2670
2671 // Otherwise, scan til the end of the statement.
2672 getParser().EatToEndOfStatement();
2673 }
2674
2675 if (getParser().MacroMap.lookup(Name)) {
2676 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2677 }
2678
2679 const char *BodyStart = StartToken.getLoc().getPointer();
2680 const char *BodyEnd = EndToken.getLoc().getPointer();
2681 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002682 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002683 return false;
2684}
2685
2686/// ParseDirectiveEndMacro
2687/// ::= .endm
2688/// ::= .endmacro
2689bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2690 SMLoc DirectiveLoc) {
2691 if (getLexer().isNot(AsmToken::EndOfStatement))
2692 return TokError("unexpected token in '" + Directive + "' directive");
2693
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002694 // If we are inside a macro instantiation, terminate the current
2695 // instantiation.
2696 if (!getParser().ActiveMacros.empty()) {
2697 getParser().HandleMacroExit();
2698 return false;
2699 }
2700
2701 // Otherwise, this .endmacro is a stray entry in the file; well formed
2702 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002703 return TokError("unexpected '" + Directive + "' in file, "
2704 "no current macro definition");
2705}
2706
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002707bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002708 getParser().CheckForValidSection();
2709
2710 const MCExpr *Value;
2711
2712 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002713 return true;
2714
2715 if (getLexer().isNot(AsmToken::EndOfStatement))
2716 return TokError("unexpected token in directive");
2717
2718 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002719 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002720 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002721 getStreamer().EmitULEB128Value(Value);
2722
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002723 return false;
2724}
2725
2726
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002727/// \brief Create an MCAsmParser instance.
2728MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2729 MCContext &C, MCStreamer &Out,
2730 const MCAsmInfo &MAI) {
2731 return new AsmParser(T, SM, C, Out, MAI);
2732}