blob: 556fedbc4d45cb6cf2d848349e880ed0a704378c [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"
Evan Chenga7cfc082011-07-23 00:45:41 +000031#include "llvm/MC/TargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000032#include "llvm/Support/CommandLine.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000033#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000034#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000037#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000038#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000039using namespace llvm;
40
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000041static cl::opt<bool>
42FatalAssemblerWarnings("fatal-assembler-warnings",
43 cl::desc("Consider warnings as error"));
44
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000045namespace {
46
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000047/// \brief Helper class for tracking macro definitions.
48struct Macro {
49 StringRef Name;
50 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000051 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000052
53public:
Rafael Espindola65366442011-06-05 02:43:45 +000054 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
55 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000056};
57
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000058/// \brief Helper class for storing information about an active macro
59/// instantiation.
60struct MacroInstantiation {
61 /// The macro being instantiated.
62 const Macro *TheMacro;
63
64 /// The macro instantiation with substitutions.
65 MemoryBuffer *Instantiation;
66
67 /// The location of the instantiation.
68 SMLoc InstantiationLoc;
69
70 /// The location where parsing should resume upon instantiation completion.
71 SMLoc ExitLoc;
72
73public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000074 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000075 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000076};
77
Daniel Dunbaraef87e32010-07-18 18:31:38 +000078/// \brief The concrete assembly parser instance.
79class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000080 friend class GenericAsmParser;
81
Daniel Dunbaraef87e32010-07-18 18:31:38 +000082 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
83 void operator=(const AsmParser &); // DO NOT IMPLEMENT
84private:
85 AsmLexer Lexer;
86 MCContext &Ctx;
87 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000088 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089 SourceMgr &SrcMgr;
90 MCAsmParserExtension *GenericParser;
91 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000092
Daniel Dunbaraef87e32010-07-18 18:31:38 +000093 /// This is the current buffer index we're lexing from as managed by the
94 /// SourceMgr object.
95 int CurBuffer;
96
97 AsmCond TheCondState;
98 std::vector<AsmCond> TheCondStack;
99
100 /// DirectiveMap - This is a table handlers for directives. Each handler is
101 /// invoked after the directive identifier is read and is responsible for
102 /// parsing and validating the rest of the directive. The handler is passed
103 /// in the directive name and the location of the directive keyword.
104 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000105
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000106 /// MacroMap - Map of currently defined macros.
107 StringMap<Macro*> MacroMap;
108
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000109 /// ActiveMacros - Stack of active macro instantiations.
110 std::vector<MacroInstantiation*> ActiveMacros;
111
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000112 /// Boolean tracking whether macro substitution is enabled.
113 unsigned MacrosEnabled : 1;
114
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000115 /// Flag tracking whether any errors have been encountered.
116 unsigned HadError : 1;
117
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000118public:
119 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
120 const MCAsmInfo &MAI);
121 ~AsmParser();
122
123 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
124
125 void AddDirectiveHandler(MCAsmParserExtension *Object,
126 StringRef Directive,
127 DirectiveHandler Handler) {
128 DirectiveMap[Directive] = std::make_pair(Object, Handler);
129 }
130
131public:
132 /// @name MCAsmParser Interface
133 /// {
134
135 virtual SourceMgr &getSourceManager() { return SrcMgr; }
136 virtual MCAsmLexer &getLexer() { return Lexer; }
137 virtual MCContext &getContext() { return Ctx; }
138 virtual MCStreamer &getStreamer() { return Out; }
139
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000140 virtual bool Warning(SMLoc L, const Twine &Msg);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000141 virtual bool Error(SMLoc L, const Twine &Msg);
142
143 const AsmToken &Lex();
144
145 bool ParseExpression(const MCExpr *&Res);
146 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
147 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
148 virtual bool ParseAbsoluteExpression(int64_t &Res);
149
150 /// }
151
152private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000153 void CheckForValidSection();
154
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000155 bool ParseStatement();
156
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000157 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000158 bool expandMacro(SmallString<256> &Buf, StringRef Body,
159 const std::vector<StringRef> &Parameters,
160 const std::vector<std::vector<AsmToken> > &A,
161 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000162 void HandleMacroExit();
163
164 void PrintMacroInstantiations();
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000165 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type,
166 bool ShowLine = true) const {
167 SrcMgr.PrintMessage(Loc, Msg, Type, ShowLine);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000168 }
169
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000170 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
171 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000172
173 /// \brief Reset the current lexer position to that given by \arg Loc. The
174 /// current token is not set; clients should ensure Lex() is called
175 /// subsequently.
176 void JumpToLoc(SMLoc Loc);
177
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000178 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000179
180 /// \brief Parse up to the end of statement and a return the contents from the
181 /// current token until the end of the statement; the current token on exit
182 /// will be either the EndOfStatement or EOF.
183 StringRef ParseStringToEndOfStatement();
184
Nico Weber4c4c7322011-01-28 03:04:41 +0000185 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000186
187 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
188 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
189 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000190 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000191
192 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
193 /// and set \arg Res to the identifier contents.
194 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000195
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000196 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000197
198 // ".ascii", ".asciiz", ".string"
199 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000200 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000201 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000202 bool ParseDirectiveFill(); // ".fill"
203 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000204 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000205 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000206 bool ParseDirectiveOrg(); // ".org"
207 // ".align{,32}", ".p2align{,w,l}"
208 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
209
210 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
211 /// accepts a single symbol (which should be a label or an external).
212 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000213
214 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
215
216 bool ParseDirectiveAbort(); // ".abort"
217 bool ParseDirectiveInclude(); // ".include"
218
219 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000220 // ".ifdef" or ".ifndef", depending on expect_defined
221 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000222 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
223 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
224 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
225
226 /// ParseEscapedString - Parse the current token as a string which may include
227 /// escaped characters and return the string contents.
228 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000229
230 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
231 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000232};
233
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000234/// \brief Generic implementations of directive handling, etc. which is shared
235/// (or the default, at least) for all assembler parser.
236class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000237 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
238 void AddDirectiveHandler(StringRef Directive) {
239 getParser().AddDirectiveHandler(this, Directive,
240 HandleDirective<GenericAsmParser, Handler>);
241 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000242public:
243 GenericAsmParser() {}
244
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000245 AsmParser &getParser() {
246 return (AsmParser&) this->MCAsmParserExtension::getParser();
247 }
248
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000249 virtual void Initialize(MCAsmParser &Parser) {
250 // Call the base implementation.
251 this->MCAsmParserExtension::Initialize(Parser);
252
253 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000254 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
255 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
256 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000257 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000258
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000259 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000260 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
261 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000262 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
263 ".cfi_startproc");
264 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
265 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
267 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
269 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000270 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
271 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
273 ".cfi_def_cfa_register");
274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
275 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
277 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000278 AddDirectiveHandler<
279 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
280 AddDirectiveHandler<
281 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000282 AddDirectiveHandler<
283 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
284 AddDirectiveHandler<
285 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000286 AddDirectiveHandler<
287 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000288
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000289 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000290 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
291 ".macros_on");
292 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
293 ".macros_off");
294 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
295 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
296 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000297
298 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
299 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000300 }
301
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000302 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
303
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000304 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
305 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
306 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000307 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000308 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000309 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
310 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000311 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000312 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000313 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000314 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
315 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000316 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000317 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000318 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
319 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000320 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000321
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000322 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000323 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
324 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000325
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000326 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000327};
328
329}
330
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000331namespace llvm {
332
333extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000334extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000335extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000336
337}
338
Chris Lattneraaec2052010-01-19 19:46:13 +0000339enum { DEFAULT_ADDRSPACE = 0 };
340
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000341AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
342 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000343 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000344 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000345 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000346 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000347
348 // Initialize the generic parser.
349 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000350
351 // Initialize the platform / file format parser.
352 //
353 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
354 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000355 if (_MAI.hasMicrosoftFastStdCallMangling()) {
356 PlatformParser = createCOFFAsmParser();
357 PlatformParser->Initialize(*this);
358 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000359 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000360 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000361 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000362 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000363 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000364 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000365}
366
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000367AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000368 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
369
370 // Destroy any macros.
371 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
372 ie = MacroMap.end(); it != ie; ++it)
373 delete it->getValue();
374
Daniel Dunbare4749702010-07-12 18:12:02 +0000375 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000376 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000377}
378
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000379void AsmParser::PrintMacroInstantiations() {
380 // Print the active macro instantiation stack.
381 for (std::vector<MacroInstantiation*>::const_reverse_iterator
382 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
383 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
384 "note");
385}
386
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000387bool AsmParser::Warning(SMLoc L, const Twine &Msg) {
388 if (FatalAssemblerWarnings)
389 return Error(L, Msg);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000390 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000391 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000392 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000393}
394
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000395bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000396 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000397 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000398 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000399 return true;
400}
401
Sean Callananfd0b0282010-01-21 00:19:58 +0000402bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000403 std::string IncludedFile;
404 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000405 if (NewBuf == -1)
406 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000407
Sean Callananfd0b0282010-01-21 00:19:58 +0000408 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000409
Sean Callananfd0b0282010-01-21 00:19:58 +0000410 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000411
Sean Callananfd0b0282010-01-21 00:19:58 +0000412 return false;
413}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000414
415void AsmParser::JumpToLoc(SMLoc Loc) {
416 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
417 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
418}
419
Sean Callananfd0b0282010-01-21 00:19:58 +0000420const AsmToken &AsmParser::Lex() {
421 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000422
Sean Callananfd0b0282010-01-21 00:19:58 +0000423 if (tok->is(AsmToken::Eof)) {
424 // If this is the end of an included file, pop the parent file off the
425 // include stack.
426 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
427 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000428 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000429 tok = &Lexer.Lex();
430 }
431 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000432
Sean Callananfd0b0282010-01-21 00:19:58 +0000433 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000434 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000435
Sean Callananfd0b0282010-01-21 00:19:58 +0000436 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000437}
438
Chris Lattner79180e22010-04-05 23:15:42 +0000439bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000440 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000441 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000442 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000443
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000444 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000445 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000446
447 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000448 AsmCond StartingCondState = TheCondState;
449
Chris Lattnerb717fb02009-07-02 21:53:43 +0000450 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000451 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000452 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000453
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000454 // We had an error, validate that one was emitted and recover by skipping to
455 // the next line.
456 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000457 EatToEndOfStatement();
458 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000459
460 if (TheCondState.TheCond != StartingCondState.TheCond ||
461 TheCondState.Ignore != StartingCondState.Ignore)
462 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000463
464 // Check to see there are no empty DwarfFile slots.
465 const std::vector<MCDwarfFile *> &MCDwarfFiles =
466 getContext().getMCDwarfFiles();
467 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000468 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000469 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000470 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000471
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000472 // Check to see that all assembler local symbols were actually defined.
473 // Targets that don't do subsections via symbols may not want this, though,
474 // so conservatively exclude them. Only do this if we're finalizing, though,
475 // as otherwise we won't necessarilly have seen everything yet.
476 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
477 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
478 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
479 e = Symbols.end();
480 i != e; ++i) {
481 MCSymbol *Sym = i->getValue();
482 // Variable symbols may not be marked as defined, so check those
483 // explicitly. If we know it's a variable, we have a definition for
484 // the purposes of this check.
485 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
486 // FIXME: We would really like to refer back to where the symbol was
487 // first referenced for a source location. We need to add something
488 // to track that. Currently, we just point to the end of the file.
489 PrintMessage(getLexer().getLoc(), "assembler local symbol '" +
490 Sym->getName() + "' not defined", "error", false);
491 }
492 }
493
494
Chris Lattner79180e22010-04-05 23:15:42 +0000495 // Finalize the output stream if there are no errors and if the client wants
496 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000497 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000498 Out.Finish();
499
Chris Lattnerb717fb02009-07-02 21:53:43 +0000500 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000501}
502
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000503void AsmParser::CheckForValidSection() {
504 if (!getStreamer().getCurrentSection()) {
505 TokError("expected section directive before assembly directive");
506 Out.SwitchSection(Ctx.getMachOSection(
507 "__TEXT", "__text",
508 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
509 0, SectionKind::getText()));
510 }
511}
512
Chris Lattner2cf5f142009-06-22 01:29:09 +0000513/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
514void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000515 while (Lexer.isNot(AsmToken::EndOfStatement) &&
516 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000517 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000518
Chris Lattner2cf5f142009-06-22 01:29:09 +0000519 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000520 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000521 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000522}
523
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000524StringRef AsmParser::ParseStringToEndOfStatement() {
525 const char *Start = getTok().getLoc().getPointer();
526
527 while (Lexer.isNot(AsmToken::EndOfStatement) &&
528 Lexer.isNot(AsmToken::Eof))
529 Lex();
530
531 const char *End = getTok().getLoc().getPointer();
532 return StringRef(Start, End - Start);
533}
Chris Lattnerc4193832009-06-22 05:51:26 +0000534
Chris Lattner74ec1a32009-06-22 06:32:03 +0000535/// ParseParenExpr - Parse a paren expression and return it.
536/// NOTE: This assumes the leading '(' has already been consumed.
537///
538/// parenexpr ::= expr)
539///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000540bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000541 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000542 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000543 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000544 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000545 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000546 return false;
547}
Chris Lattnerc4193832009-06-22 05:51:26 +0000548
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000549/// ParseBracketExpr - Parse a bracket expression and return it.
550/// NOTE: This assumes the leading '[' has already been consumed.
551///
552/// bracketexpr ::= expr]
553///
554bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
555 if (ParseExpression(Res)) return true;
556 if (Lexer.isNot(AsmToken::RBrac))
557 return TokError("expected ']' in brackets expression");
558 EndLoc = Lexer.getLoc();
559 Lex();
560 return false;
561}
562
Chris Lattner74ec1a32009-06-22 06:32:03 +0000563/// ParsePrimaryExpr - Parse a primary expression and return it.
564/// primaryexpr ::= (parenexpr
565/// primaryexpr ::= symbol
566/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000567/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000568/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000569bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000570 switch (Lexer.getKind()) {
571 default:
572 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000573 // If we have an error assume that we've already handled it.
574 case AsmToken::Error:
575 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000576 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000577 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000578 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000579 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000580 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000581 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000582 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000583 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000584 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000585 EndLoc = Lexer.getLoc();
586
587 StringRef Identifier;
588 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000589 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000590
Daniel Dunbarfffff912009-10-16 01:34:54 +0000591 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000592 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000593 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000594
595 // Lookup the symbol variant if used.
596 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000597 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000598 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000599 if (Variant == MCSymbolRefExpr::VK_Invalid) {
600 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000601 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000602 }
603 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000604
Daniel Dunbarfffff912009-10-16 01:34:54 +0000605 // If this is an absolute variable reference, substitute it now to preserve
606 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000607 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000608 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000609 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000610
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000611 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000612 return false;
613 }
614
615 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000616 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000617 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000618 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000619 case AsmToken::Integer: {
620 SMLoc Loc = getTok().getLoc();
621 int64_t IntVal = getTok().getIntVal();
622 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000623 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000624 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000625 // Look for 'b' or 'f' following an Integer as a directional label
626 if (Lexer.getKind() == AsmToken::Identifier) {
627 StringRef IDVal = getTok().getString();
628 if (IDVal == "f" || IDVal == "b"){
629 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
630 IDVal == "f" ? 1 : 0);
631 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
632 getContext());
633 if(IDVal == "b" && Sym->isUndefined())
634 return Error(Loc, "invalid reference to undefined symbol");
635 EndLoc = Lexer.getLoc();
636 Lex(); // Eat identifier.
637 }
638 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000639 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000640 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000641 case AsmToken::Real: {
642 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000643 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000644 Res = MCConstantExpr::Create(IntVal, getContext());
645 Lex(); // Eat token.
646 return false;
647 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000648 case AsmToken::Dot: {
649 // This is a '.' reference, which references the current PC. Emit a
650 // temporary label to the streamer and refer to it.
651 MCSymbol *Sym = Ctx.CreateTempSymbol();
652 Out.EmitLabel(Sym);
653 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
654 EndLoc = Lexer.getLoc();
655 Lex(); // Eat identifier.
656 return false;
657 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000658 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000659 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000660 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000661 case AsmToken::LBrac:
662 if (!PlatformParser->HasBracketExpressions())
663 return TokError("brackets expression not supported on this target");
664 Lex(); // Eat the '['.
665 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000666 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000667 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000668 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000669 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000670 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000671 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000672 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000673 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000674 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000675 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000676 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000677 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000678 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000679 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000680 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000681 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000682 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000683 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000684 }
685}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000686
Chris Lattnerb4307b32010-01-15 19:28:38 +0000687bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000688 SMLoc EndLoc;
689 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000690}
691
Daniel Dunbarcceba832010-09-17 02:47:07 +0000692const MCExpr *
693AsmParser::ApplyModifierToExpr(const MCExpr *E,
694 MCSymbolRefExpr::VariantKind Variant) {
695 // Recurse over the given expression, rebuilding it to apply the given variant
696 // if there is exactly one symbol.
697 switch (E->getKind()) {
698 case MCExpr::Target:
699 case MCExpr::Constant:
700 return 0;
701
702 case MCExpr::SymbolRef: {
703 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
704
705 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
706 TokError("invalid variant on expression '" +
707 getTok().getIdentifier() + "' (already modified)");
708 return E;
709 }
710
711 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
712 }
713
714 case MCExpr::Unary: {
715 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
716 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
717 if (!Sub)
718 return 0;
719 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
720 }
721
722 case MCExpr::Binary: {
723 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
724 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
725 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
726
727 if (!LHS && !RHS)
728 return 0;
729
730 if (!LHS) LHS = BE->getLHS();
731 if (!RHS) RHS = BE->getRHS();
732
733 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
734 }
735 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000736
737 assert(0 && "Invalid expression kind!");
738 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000739}
740
Chris Lattner74ec1a32009-06-22 06:32:03 +0000741/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000742///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000743/// expr ::= expr +,- expr -> lowest.
744/// expr ::= expr |,^,&,! expr -> middle.
745/// expr ::= expr *,/,%,<<,>> expr -> highest.
746/// expr ::= primaryexpr
747///
Chris Lattner54482b42010-01-15 19:39:23 +0000748bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000749 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000750 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000751 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
752 return true;
753
Daniel Dunbarcceba832010-09-17 02:47:07 +0000754 // As a special case, we support 'a op b @ modifier' by rewriting the
755 // expression to include the modifier. This is inefficient, but in general we
756 // expect users to use 'a@modifier op b'.
757 if (Lexer.getKind() == AsmToken::At) {
758 Lex();
759
760 if (Lexer.isNot(AsmToken::Identifier))
761 return TokError("unexpected symbol modifier following '@'");
762
763 MCSymbolRefExpr::VariantKind Variant =
764 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
765 if (Variant == MCSymbolRefExpr::VK_Invalid)
766 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
767
768 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
769 if (!ModifiedRes) {
770 return TokError("invalid modifier '" + getTok().getIdentifier() +
771 "' (no symbols present)");
772 return true;
773 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000774
Daniel Dunbarcceba832010-09-17 02:47:07 +0000775 Res = ModifiedRes;
776 Lex();
777 }
778
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000779 // Try to constant fold it up front, if possible.
780 int64_t Value;
781 if (Res->EvaluateAsAbsolute(Value))
782 Res = MCConstantExpr::Create(Value, getContext());
783
784 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000785}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000786
Chris Lattnerb4307b32010-01-15 19:28:38 +0000787bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000788 Res = 0;
789 return ParseParenExpr(Res, EndLoc) ||
790 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000791}
792
Daniel Dunbar475839e2009-06-29 20:37:27 +0000793bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000794 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000795
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000796 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000797 if (ParseExpression(Expr))
798 return true;
799
Daniel Dunbare00b0112009-10-16 01:57:52 +0000800 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000801 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000802
803 return false;
804}
805
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000806static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000807 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000808 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000809 default:
810 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000811
Daniel Dunbarcceba832010-09-17 02:47:07 +0000812 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000813 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000814 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000815 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000816 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000817 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000818 return 1;
819
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000820
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000821 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000822 //
823 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000824 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000825 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000826 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000827 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000828 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000829 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000830 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000831 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000832 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000833
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000834 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000835 case AsmToken::EqualEqual:
836 Kind = MCBinaryExpr::EQ;
837 return 3;
838 case AsmToken::ExclaimEqual:
839 case AsmToken::LessGreater:
840 Kind = MCBinaryExpr::NE;
841 return 3;
842 case AsmToken::Less:
843 Kind = MCBinaryExpr::LT;
844 return 3;
845 case AsmToken::LessEqual:
846 Kind = MCBinaryExpr::LTE;
847 return 3;
848 case AsmToken::Greater:
849 Kind = MCBinaryExpr::GT;
850 return 3;
851 case AsmToken::GreaterEqual:
852 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000853 return 3;
854
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000855 // High Intermediate Precedence: +, -
856 case AsmToken::Plus:
857 Kind = MCBinaryExpr::Add;
858 return 4;
859 case AsmToken::Minus:
860 Kind = MCBinaryExpr::Sub;
861 return 4;
862
Daniel Dunbar475839e2009-06-29 20:37:27 +0000863 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000864 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000865 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000866 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000867 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000868 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000869 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000870 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000871 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000872 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000873 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000874 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000875 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000876 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000877 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000878 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000879 }
880}
881
882
883/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
884/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000885bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
886 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000887 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000888 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000889 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000890
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000891 // If the next token is lower precedence than we are allowed to eat, return
892 // successfully with what we ate already.
893 if (TokPrec < Precedence)
894 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000895
Sean Callanan79ed1a82010-01-19 20:22:31 +0000896 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000897
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000898 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000899 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000900 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000901
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000902 // If BinOp binds less tightly with RHS than the operator after RHS, let
903 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000904 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000905 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000906 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000907 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000908 }
909
Daniel Dunbar475839e2009-06-29 20:37:27 +0000910 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000911 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000912 }
913}
914
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000915
916
917
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000918/// ParseStatement:
919/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000920/// ::= Label* Directive ...Operands... EndOfStatement
921/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000922bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000923 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000924 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000925 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000926 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000927 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000928
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000929 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000930 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000931 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000932 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000933 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000934 // A full line comment is a '#' as the first token.
935 if (Lexer.is(AsmToken::Hash)) {
936 EatToEndOfStatement();
937 return false;
938 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000939
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000940 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000941 if (Lexer.is(AsmToken::Integer)) {
942 LocalLabelVal = getTok().getIntVal();
943 if (LocalLabelVal < 0) {
944 if (!TheCondState.Ignore)
945 return TokError("unexpected token at start of statement");
946 IDVal = "";
947 }
948 else {
949 IDVal = getTok().getString();
950 Lex(); // Consume the integer token to be used as an identifier token.
951 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000952 if (!TheCondState.Ignore)
953 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000954 }
955 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000956
957 } else if (Lexer.is(AsmToken::Dot)) {
958 // Treat '.' as a valid identifier in this context.
959 Lex();
960 IDVal = ".";
961
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000962 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000963 if (!TheCondState.Ignore)
964 return TokError("unexpected token at start of statement");
965 IDVal = "";
966 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000967
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000968
Chris Lattner7834fac2010-04-17 18:14:27 +0000969 // Handle conditional assembly here before checking for skipping. We
970 // have to do this so that .endif isn't skipped in a ".if 0" block for
971 // example.
972 if (IDVal == ".if")
973 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000974 if (IDVal == ".ifdef")
975 return ParseDirectiveIfdef(IDLoc, true);
976 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
977 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000978 if (IDVal == ".elseif")
979 return ParseDirectiveElseIf(IDLoc);
980 if (IDVal == ".else")
981 return ParseDirectiveElse(IDLoc);
982 if (IDVal == ".endif")
983 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000984
Chris Lattner7834fac2010-04-17 18:14:27 +0000985 // If we are in a ".if 0" block, ignore this statement.
986 if (TheCondState.Ignore) {
987 EatToEndOfStatement();
988 return false;
989 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000990
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000991 // FIXME: Recurse on local labels?
992
993 // See what kind of statement we have.
994 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000995 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000996 CheckForValidSection();
997
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000998 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000999 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001000
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001001 // Diagnose attempt to use '.' as a label.
1002 if (IDVal == ".")
1003 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1004
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001005 // Diagnose attempt to use a variable as a label.
1006 //
1007 // FIXME: Diagnostics. Note the location of the definition as a label.
1008 // FIXME: This doesn't diagnose assignment to a symbol which has been
1009 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001010 MCSymbol *Sym;
1011 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001012 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001013 else
1014 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001015 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001016 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001017
Daniel Dunbar959fd882009-08-26 22:13:22 +00001018 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001019 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001020
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001021 // Consume any end of statement token, if present, to avoid spurious
1022 // AddBlankLine calls().
1023 if (Lexer.is(AsmToken::EndOfStatement)) {
1024 Lex();
1025 if (Lexer.is(AsmToken::Eof))
1026 return false;
1027 }
1028
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001029 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001030 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001031
Daniel Dunbar3f872332009-07-28 16:08:33 +00001032 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001033 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001034 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001035
Nico Weber4c4c7322011-01-28 03:04:41 +00001036 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001037
1038 default: // Normal instruction or directive.
1039 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001040 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001041
1042 // If macros are enabled, check to see if this is a macro instantiation.
1043 if (MacrosEnabled)
1044 if (const Macro *M = MacroMap.lookup(IDVal))
1045 return HandleMacroEntry(IDVal, IDLoc, M);
1046
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001047 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001048 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001049 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001050 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001051 return ParseDirectiveSet(IDVal, true);
1052 if (IDVal == ".equiv")
1053 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001054
Daniel Dunbara0d14262009-06-24 23:30:00 +00001055 // Data directives
1056
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001057 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001058 return ParseDirectiveAscii(IDVal, false);
1059 if (IDVal == ".asciz" || IDVal == ".string")
1060 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001061
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001062 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001063 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001064 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001065 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001066 if (IDVal == ".value")
1067 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001068 if (IDVal == ".2byte")
1069 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001070 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001071 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001072 if (IDVal == ".int")
1073 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001074 if (IDVal == ".4byte")
1075 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001076 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001078 if (IDVal == ".8byte")
1079 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001080 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001081 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1082 if (IDVal == ".double")
1083 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001084
Eli Friedman5d68ec22010-07-19 04:17:25 +00001085 if (IDVal == ".align") {
1086 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1087 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1088 }
1089 if (IDVal == ".align32") {
1090 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1091 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1092 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001093 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001094 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001095 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001096 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001097 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001098 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001099 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001100 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001101 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001102 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001103 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001104 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1105
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001106 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001107 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001108
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001109 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001110 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001111 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001112 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001113 if (IDVal == ".zero")
1114 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001115
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001116 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001117
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001118 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001119 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001120 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001121 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001122 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001123 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001125 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001126 if (IDVal == ".symbol_resolver")
1127 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001128 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001129 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001130 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001131 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001132 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001133 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001134 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001135 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001136 if (IDVal == ".weak_def_can_be_hidden")
1137 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001138
Hans Wennborg5cc64912011-06-18 13:51:54 +00001139 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001140 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001141 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001142 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001143
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001144 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001145 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001146 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001147 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001148
Roman Divackybb6d14f2011-01-31 21:19:43 +00001149 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001150 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001151
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001152 // Look up the handler in the handler table.
1153 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1154 DirectiveMap.lookup(IDVal);
1155 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001156 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001157
Kevin Enderby9c656452009-09-10 20:51:44 +00001158 // Target hook for parsing target specific directives.
1159 if (!getTargetParser().ParseDirective(ID))
1160 return false;
1161
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001162 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001163 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001164 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001165 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001166
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001167 CheckForValidSection();
1168
Chris Lattnera7f13542010-05-19 23:34:33 +00001169 // Canonicalize the opcode to lower case.
1170 SmallString<128> Opcode;
1171 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1172 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001173
Chris Lattner98986712010-01-14 22:21:20 +00001174 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001175 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001176 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001177
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001178 // Dump the parsed representation, if requested.
1179 if (getShowParsedOperands()) {
1180 SmallString<256> Str;
1181 raw_svector_ostream OS(Str);
1182 OS << "parsed instruction: [";
1183 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1184 if (i != 0)
1185 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001186 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001187 }
1188 OS << "]";
1189
1190 PrintMessage(IDLoc, OS.str(), "note");
1191 }
1192
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001193 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001194 if (!HadError)
1195 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1196 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001197
Chris Lattner98986712010-01-14 22:21:20 +00001198 // Free any parsed operands.
1199 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1200 delete ParsedOperands[i];
1201
Chris Lattnercbf8a982010-09-11 16:18:25 +00001202 // Don't skip the rest of the line, the instruction parser is responsible for
1203 // that.
1204 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001205}
Chris Lattner9a023f72009-06-24 04:43:34 +00001206
Rafael Espindola65366442011-06-05 02:43:45 +00001207bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1208 const std::vector<StringRef> &Parameters,
1209 const std::vector<std::vector<AsmToken> > &A,
1210 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001211 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001212 unsigned NParameters = Parameters.size();
1213 if (NParameters != 0 && NParameters != A.size())
1214 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001215
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001216 while (!Body.empty()) {
1217 // Scan for the next substitution.
1218 std::size_t End = Body.size(), Pos = 0;
1219 for (; Pos != End; ++Pos) {
1220 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001221 if (!NParameters) {
1222 // This macro has no parameters, look for $0, $1, etc.
1223 if (Body[Pos] != '$' || Pos + 1 == End)
1224 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001225
Rafael Espindola65366442011-06-05 02:43:45 +00001226 char Next = Body[Pos + 1];
1227 if (Next == '$' || Next == 'n' || isdigit(Next))
1228 break;
1229 } else {
1230 // This macro has parameters, look for \foo, \bar, etc.
1231 if (Body[Pos] == '\\' && Pos + 1 != End)
1232 break;
1233 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001234 }
1235
1236 // Add the prefix.
1237 OS << Body.slice(0, Pos);
1238
1239 // Check if we reached the end.
1240 if (Pos == End)
1241 break;
1242
Rafael Espindola65366442011-06-05 02:43:45 +00001243 if (!NParameters) {
1244 switch (Body[Pos+1]) {
1245 // $$ => $
1246 case '$':
1247 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001248 break;
1249
Rafael Espindola65366442011-06-05 02:43:45 +00001250 // $n => number of arguments
1251 case 'n':
1252 OS << A.size();
1253 break;
1254
1255 // $[0-9] => argument
1256 default: {
1257 // Missing arguments are ignored.
1258 unsigned Index = Body[Pos+1] - '0';
1259 if (Index >= A.size())
1260 break;
1261
1262 // Otherwise substitute with the token values, with spaces eliminated.
1263 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1264 ie = A[Index].end(); it != ie; ++it)
1265 OS << it->getString();
1266 break;
1267 }
1268 }
1269 Pos += 2;
1270 } else {
1271 unsigned I = Pos + 1;
1272 while (isalnum(Body[I]) && I + 1 != End)
1273 ++I;
1274
1275 const char *Begin = Body.data() + Pos +1;
1276 StringRef Argument(Begin, I - (Pos +1));
1277 unsigned Index = 0;
1278 for (; Index < NParameters; ++Index)
1279 if (Parameters[Index] == Argument)
1280 break;
1281
1282 // FIXME: We should error at the macro definition.
1283 if (Index == NParameters)
1284 return Error(L, "Parameter not found");
1285
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001286 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1287 ie = A[Index].end(); it != ie; ++it)
1288 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001289
Rafael Espindola65366442011-06-05 02:43:45 +00001290 Pos += 1 + Argument.size();
1291 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001292 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001293 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001294 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001295
1296 // We include the .endmacro in the buffer as our queue to exit the macro
1297 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001298 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001299 return false;
1300}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001301
Rafael Espindola65366442011-06-05 02:43:45 +00001302MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1303 MemoryBuffer *I)
1304 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1305{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001306}
1307
1308bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1309 const Macro *M) {
1310 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1311 // this, although we should protect against infinite loops.
1312 if (ActiveMacros.size() == 20)
1313 return TokError("macros cannot be nested more than 20 levels deep");
1314
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001315 // Parse the macro instantiation arguments.
1316 std::vector<std::vector<AsmToken> > MacroArguments;
1317 MacroArguments.push_back(std::vector<AsmToken>());
1318 unsigned ParenLevel = 0;
1319 for (;;) {
1320 if (Lexer.is(AsmToken::Eof))
1321 return TokError("unexpected token in macro instantiation");
1322 if (Lexer.is(AsmToken::EndOfStatement))
1323 break;
1324
1325 // If we aren't inside parentheses and this is a comma, start a new token
1326 // list.
1327 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1328 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001329 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001330 // Adjust the current parentheses level.
1331 if (Lexer.is(AsmToken::LParen))
1332 ++ParenLevel;
1333 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1334 --ParenLevel;
1335
1336 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001337 MacroArguments.back().push_back(getTok());
1338 }
1339 Lex();
1340 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001341
Rafael Espindola65366442011-06-05 02:43:45 +00001342 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1343 // to hold the macro body with substitutions.
1344 SmallString<256> Buf;
1345 StringRef Body = M->Body;
1346
1347 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1348 return true;
1349
1350 MemoryBuffer *Instantiation =
1351 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1352
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001353 // Create the macro instantiation object and add to the current macro
1354 // instantiation stack.
1355 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001356 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001357 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001358 ActiveMacros.push_back(MI);
1359
1360 // Jump to the macro instantiation and prime the lexer.
1361 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1362 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1363 Lex();
1364
1365 return false;
1366}
1367
1368void AsmParser::HandleMacroExit() {
1369 // Jump to the EndOfStatement we should return to, and consume it.
1370 JumpToLoc(ActiveMacros.back()->ExitLoc);
1371 Lex();
1372
1373 // Pop the instantiation entry.
1374 delete ActiveMacros.back();
1375 ActiveMacros.pop_back();
1376}
1377
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001378static void MarkUsed(const MCExpr *Value) {
1379 switch (Value->getKind()) {
1380 case MCExpr::Binary:
1381 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1382 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1383 break;
1384 case MCExpr::Target:
1385 case MCExpr::Constant:
1386 break;
1387 case MCExpr::SymbolRef: {
1388 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1389 break;
1390 }
1391 case MCExpr::Unary:
1392 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1393 break;
1394 }
1395}
1396
Nico Weber4c4c7322011-01-28 03:04:41 +00001397bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001398 // FIXME: Use better location, we should use proper tokens.
1399 SMLoc EqualLoc = Lexer.getLoc();
1400
Daniel Dunbar821e3332009-08-31 08:09:28 +00001401 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001402 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001403 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001404
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001405 MarkUsed(Value);
1406
Daniel Dunbar3f872332009-07-28 16:08:33 +00001407 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001408 return TokError("unexpected token in assignment");
1409
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001410 // Error on assignment to '.'.
1411 if (Name == ".") {
1412 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1413 "(use '.space' or '.org').)"));
1414 }
1415
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001416 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001417 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001418
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001419 // Validate that the LHS is allowed to be a variable (either it has not been
1420 // used as a symbol, or it is an absolute symbol).
1421 MCSymbol *Sym = getContext().LookupSymbol(Name);
1422 if (Sym) {
1423 // Diagnose assignment to a label.
1424 //
1425 // FIXME: Diagnostics. Note the location of the definition as a label.
1426 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001427 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001428 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001429 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001430 return Error(EqualLoc, "redefinition of '" + Name + "'");
1431 else if (!Sym->isVariable())
1432 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001433 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001434 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1435 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001436
1437 // Don't count these checks as uses.
1438 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001439 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001440 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001441
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001442 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001443
1444 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001445 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001446
1447 return false;
1448}
1449
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001450/// ParseIdentifier:
1451/// ::= identifier
1452/// ::= string
1453bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001454 // The assembler has relaxed rules for accepting identifiers, in particular we
1455 // allow things like '.globl $foo', which would normally be separate
1456 // tokens. At this level, we have already lexed so we cannot (currently)
1457 // handle this as a context dependent token, instead we detect adjacent tokens
1458 // and return the combined identifier.
1459 if (Lexer.is(AsmToken::Dollar)) {
1460 SMLoc DollarLoc = getLexer().getLoc();
1461
1462 // Consume the dollar sign, and check for a following identifier.
1463 Lex();
1464 if (Lexer.isNot(AsmToken::Identifier))
1465 return true;
1466
1467 // We have a '$' followed by an identifier, make sure they are adjacent.
1468 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1469 return true;
1470
1471 // Construct the joined identifier and consume the token.
1472 Res = StringRef(DollarLoc.getPointer(),
1473 getTok().getIdentifier().size() + 1);
1474 Lex();
1475 return false;
1476 }
1477
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001478 if (Lexer.isNot(AsmToken::Identifier) &&
1479 Lexer.isNot(AsmToken::String))
1480 return true;
1481
Sean Callanan18b83232010-01-19 21:44:56 +00001482 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001483
Sean Callanan79ed1a82010-01-19 20:22:31 +00001484 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001485
1486 return false;
1487}
1488
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001489/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001490/// ::= .equ identifier ',' expression
1491/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001492/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001493bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001494 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001495
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001496 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001497 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001498
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001499 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001500 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001501 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001502
Nico Weber4c4c7322011-01-28 03:04:41 +00001503 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001504}
1505
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001506bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001507 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001508
1509 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001510 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001511 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1512 if (Str[i] != '\\') {
1513 Data += Str[i];
1514 continue;
1515 }
1516
1517 // Recognize escaped characters. Note that this escape semantics currently
1518 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1519 ++i;
1520 if (i == e)
1521 return TokError("unexpected backslash at end of string");
1522
1523 // Recognize octal sequences.
1524 if ((unsigned) (Str[i] - '0') <= 7) {
1525 // Consume up to three octal characters.
1526 unsigned Value = Str[i] - '0';
1527
1528 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1529 ++i;
1530 Value = Value * 8 + (Str[i] - '0');
1531
1532 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1533 ++i;
1534 Value = Value * 8 + (Str[i] - '0');
1535 }
1536 }
1537
1538 if (Value > 255)
1539 return TokError("invalid octal escape sequence (out of range)");
1540
1541 Data += (unsigned char) Value;
1542 continue;
1543 }
1544
1545 // Otherwise recognize individual escapes.
1546 switch (Str[i]) {
1547 default:
1548 // Just reject invalid escape sequences for now.
1549 return TokError("invalid escape sequence (unrecognized character)");
1550
1551 case 'b': Data += '\b'; break;
1552 case 'f': Data += '\f'; break;
1553 case 'n': Data += '\n'; break;
1554 case 'r': Data += '\r'; break;
1555 case 't': Data += '\t'; break;
1556 case '"': Data += '"'; break;
1557 case '\\': Data += '\\'; break;
1558 }
1559 }
1560
1561 return false;
1562}
1563
Daniel Dunbara0d14262009-06-24 23:30:00 +00001564/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001565/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1566bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001567 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001568 CheckForValidSection();
1569
Daniel Dunbara0d14262009-06-24 23:30:00 +00001570 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001571 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001572 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001573
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001574 std::string Data;
1575 if (ParseEscapedString(Data))
1576 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001577
1578 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001579 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001580 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1581
Sean Callanan79ed1a82010-01-19 20:22:31 +00001582 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001583
1584 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001585 break;
1586
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001587 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001588 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001589 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001590 }
1591 }
1592
Sean Callanan79ed1a82010-01-19 20:22:31 +00001593 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001594 return false;
1595}
1596
1597/// ParseDirectiveValue
1598/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1599bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001600 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001601 CheckForValidSection();
1602
Daniel Dunbara0d14262009-06-24 23:30:00 +00001603 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001604 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001605 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001606 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001607 return true;
1608
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001609 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001610 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1611 assert(Size <= 8 && "Invalid size");
1612 uint64_t IntValue = MCE->getValue();
1613 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1614 return Error(ExprLoc, "literal value out of range for directive");
1615 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1616 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001617 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001618
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001619 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001620 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001621
Daniel Dunbara0d14262009-06-24 23:30:00 +00001622 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001623 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001624 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001625 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001626 }
1627 }
1628
Sean Callanan79ed1a82010-01-19 20:22:31 +00001629 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001630 return false;
1631}
1632
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001633/// ParseDirectiveRealValue
1634/// ::= (.single | .double) [ expression (, expression)* ]
1635bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1636 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1637 CheckForValidSection();
1638
1639 for (;;) {
1640 // We don't truly support arithmetic on floating point expressions, so we
1641 // have to manually parse unary prefixes.
1642 bool IsNeg = false;
1643 if (getLexer().is(AsmToken::Minus)) {
1644 Lex();
1645 IsNeg = true;
1646 } else if (getLexer().is(AsmToken::Plus))
1647 Lex();
1648
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001649 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001650 getLexer().isNot(AsmToken::Real) &&
1651 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001652 return TokError("unexpected token in directive");
1653
1654 // Convert to an APFloat.
1655 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001656 StringRef IDVal = getTok().getString();
1657 if (getLexer().is(AsmToken::Identifier)) {
1658 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1659 Value = APFloat::getInf(Semantics);
1660 else if (!IDVal.compare_lower("nan"))
1661 Value = APFloat::getNaN(Semantics, false, ~0);
1662 else
1663 return TokError("invalid floating point literal");
1664 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001665 APFloat::opInvalidOp)
1666 return TokError("invalid floating point literal");
1667 if (IsNeg)
1668 Value.changeSign();
1669
1670 // Consume the numeric token.
1671 Lex();
1672
1673 // Emit the value as an integer.
1674 APInt AsInt = Value.bitcastToAPInt();
1675 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1676 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1677
1678 if (getLexer().is(AsmToken::EndOfStatement))
1679 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001680
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001681 if (getLexer().isNot(AsmToken::Comma))
1682 return TokError("unexpected token in directive");
1683 Lex();
1684 }
1685 }
1686
1687 Lex();
1688 return false;
1689}
1690
Daniel Dunbara0d14262009-06-24 23:30:00 +00001691/// ParseDirectiveSpace
1692/// ::= .space expression [ , expression ]
1693bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001694 CheckForValidSection();
1695
Daniel Dunbara0d14262009-06-24 23:30:00 +00001696 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001697 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001698 return true;
1699
1700 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001701 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1702 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001703 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001704 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001705
Daniel Dunbar475839e2009-06-29 20:37:27 +00001706 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001707 return true;
1708
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001709 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001710 return TokError("unexpected token in '.space' directive");
1711 }
1712
Sean Callanan79ed1a82010-01-19 20:22:31 +00001713 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001714
1715 if (NumBytes <= 0)
1716 return TokError("invalid number of bytes in '.space' directive");
1717
1718 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001719 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001720
1721 return false;
1722}
1723
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001724/// ParseDirectiveZero
1725/// ::= .zero expression
1726bool AsmParser::ParseDirectiveZero() {
1727 CheckForValidSection();
1728
1729 int64_t NumBytes;
1730 if (ParseAbsoluteExpression(NumBytes))
1731 return true;
1732
Rafael Espindolae452b172010-10-05 19:42:57 +00001733 int64_t Val = 0;
1734 if (getLexer().is(AsmToken::Comma)) {
1735 Lex();
1736 if (ParseAbsoluteExpression(Val))
1737 return true;
1738 }
1739
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001740 if (getLexer().isNot(AsmToken::EndOfStatement))
1741 return TokError("unexpected token in '.zero' directive");
1742
1743 Lex();
1744
Rafael Espindolae452b172010-10-05 19:42:57 +00001745 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001746
1747 return false;
1748}
1749
Daniel Dunbara0d14262009-06-24 23:30:00 +00001750/// ParseDirectiveFill
1751/// ::= .fill expression , expression , expression
1752bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001753 CheckForValidSection();
1754
Daniel Dunbara0d14262009-06-24 23:30:00 +00001755 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001756 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001757 return true;
1758
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001759 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001760 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001761 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001762
Daniel Dunbara0d14262009-06-24 23:30:00 +00001763 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001764 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001765 return true;
1766
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001767 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001768 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001769 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001770
Daniel Dunbara0d14262009-06-24 23:30:00 +00001771 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001772 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001773 return true;
1774
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001775 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001776 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001777
Sean Callanan79ed1a82010-01-19 20:22:31 +00001778 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001779
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001780 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1781 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001782
1783 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001784 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001785
1786 return false;
1787}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001788
1789/// ParseDirectiveOrg
1790/// ::= .org expression [ , expression ]
1791bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001792 CheckForValidSection();
1793
Daniel Dunbar821e3332009-08-31 08:09:28 +00001794 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001795 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001796 return true;
1797
1798 // Parse optional fill expression.
1799 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001800 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1801 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001802 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001803 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001804
Daniel Dunbar475839e2009-06-29 20:37:27 +00001805 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001806 return true;
1807
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001808 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001809 return TokError("unexpected token in '.org' directive");
1810 }
1811
Sean Callanan79ed1a82010-01-19 20:22:31 +00001812 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001813
1814 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1815 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001816 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001817
1818 return false;
1819}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001820
1821/// ParseDirectiveAlign
1822/// ::= {.align, ...} expression [ , expression [ , expression ]]
1823bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001824 CheckForValidSection();
1825
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001826 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001827 int64_t Alignment;
1828 if (ParseAbsoluteExpression(Alignment))
1829 return true;
1830
1831 SMLoc MaxBytesLoc;
1832 bool HasFillExpr = false;
1833 int64_t FillExpr = 0;
1834 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001835 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1836 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001837 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001838 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001839
1840 // The fill expression can be omitted while specifying a maximum number of
1841 // alignment bytes, e.g:
1842 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001843 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001844 HasFillExpr = true;
1845 if (ParseAbsoluteExpression(FillExpr))
1846 return true;
1847 }
1848
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001849 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1850 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001851 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001852 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001853
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001854 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001855 if (ParseAbsoluteExpression(MaxBytesToFill))
1856 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001857
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001858 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001859 return TokError("unexpected token in directive");
1860 }
1861 }
1862
Sean Callanan79ed1a82010-01-19 20:22:31 +00001863 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001864
Daniel Dunbar648ac512010-05-17 21:54:30 +00001865 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001866 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001867
1868 // Compute alignment in bytes.
1869 if (IsPow2) {
1870 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001871 if (Alignment >= 32) {
1872 Error(AlignmentLoc, "invalid alignment value");
1873 Alignment = 31;
1874 }
1875
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001876 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001877 }
1878
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001879 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001880 if (MaxBytesLoc.isValid()) {
1881 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001882 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1883 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001884 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001885 }
1886
1887 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001888 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1889 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001890 MaxBytesToFill = 0;
1891 }
1892 }
1893
Daniel Dunbar648ac512010-05-17 21:54:30 +00001894 // Check whether we should use optimal code alignment for this .align
1895 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001896 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001897 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1898 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001899 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001900 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001901 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001902 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1903 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001904 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001905
1906 return false;
1907}
1908
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001909/// ParseDirectiveSymbolAttribute
1910/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001911bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001912 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001913 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001914 StringRef Name;
1915
1916 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001917 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001918
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001919 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001920
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001921 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001922
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001923 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001924 break;
1925
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001926 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001927 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001928 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001929 }
1930 }
1931
Sean Callanan79ed1a82010-01-19 20:22:31 +00001932 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001933 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001934}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001935
1936/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001937/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1938bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001939 CheckForValidSection();
1940
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001941 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001942 StringRef Name;
1943 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001944 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001945
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001946 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001947 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001948
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001949 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001950 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001951 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001952
1953 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001954 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001955 if (ParseAbsoluteExpression(Size))
1956 return true;
1957
1958 int64_t Pow2Alignment = 0;
1959 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001960 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001961 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001962 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001963 if (ParseAbsoluteExpression(Pow2Alignment))
1964 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001965
Chris Lattner258281d2010-01-19 06:22:22 +00001966 // If this target takes alignments in bytes (not log) validate and convert.
1967 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1968 if (!isPowerOf2_64(Pow2Alignment))
1969 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1970 Pow2Alignment = Log2_64(Pow2Alignment);
1971 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001972 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001973
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001974 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001975 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001976
Sean Callanan79ed1a82010-01-19 20:22:31 +00001977 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001978
Chris Lattner1fc3d752009-07-09 17:25:12 +00001979 // NOTE: a size of zero for a .comm should create a undefined symbol
1980 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001981 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001982 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1983 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001984
Eric Christopherc260a3e2010-05-14 01:38:54 +00001985 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001986 // may internally end up wanting an alignment in bytes.
1987 // FIXME: Diagnose overflow.
1988 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001989 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1990 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001991
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001992 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001993 return Error(IDLoc, "invalid symbol redefinition");
1994
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001995 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001996 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001997 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001998 getStreamer().EmitZerofill(Ctx.getMachOSection(
1999 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2000 0, SectionKind::getBSS()),
2001 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002002 return false;
2003 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002004
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002005 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002006 return false;
2007}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002008
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002009/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002010/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002011bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002012 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002013 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002014
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002015 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002016 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002017 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002018
Sean Callanan79ed1a82010-01-19 20:22:31 +00002019 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002020
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002021 if (Str.empty())
2022 Error(Loc, ".abort detected. Assembly stopping.");
2023 else
2024 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002025 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002026
2027 return false;
2028}
Kevin Enderby71148242009-07-14 21:35:03 +00002029
Kevin Enderby1f049b22009-07-14 23:21:55 +00002030/// ParseDirectiveInclude
2031/// ::= .include "filename"
2032bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002033 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002034 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002035
Sean Callanan18b83232010-01-19 21:44:56 +00002036 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002037 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002038 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002039
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002040 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002041 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002042
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002043 // Strip the quotes.
2044 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002045
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002046 // Attempt to switch the lexer to the included file before consuming the end
2047 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002048 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002049 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002050 return true;
2051 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002052
2053 return false;
2054}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002055
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002056/// ParseDirectiveIf
2057/// ::= .if expression
2058bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002059 TheCondStack.push_back(TheCondState);
2060 TheCondState.TheCond = AsmCond::IfCond;
2061 if(TheCondState.Ignore) {
2062 EatToEndOfStatement();
2063 }
2064 else {
2065 int64_t ExprValue;
2066 if (ParseAbsoluteExpression(ExprValue))
2067 return true;
2068
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002069 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002070 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002071
Sean Callanan79ed1a82010-01-19 20:22:31 +00002072 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002073
2074 TheCondState.CondMet = ExprValue;
2075 TheCondState.Ignore = !TheCondState.CondMet;
2076 }
2077
2078 return false;
2079}
2080
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002081bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2082 StringRef Name;
2083 TheCondStack.push_back(TheCondState);
2084 TheCondState.TheCond = AsmCond::IfCond;
2085
2086 if (TheCondState.Ignore) {
2087 EatToEndOfStatement();
2088 } else {
2089 if (ParseIdentifier(Name))
2090 return TokError("expected identifier after '.ifdef'");
2091
2092 Lex();
2093
2094 MCSymbol *Sym = getContext().LookupSymbol(Name);
2095
2096 if (expect_defined)
2097 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2098 else
2099 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2100 TheCondState.Ignore = !TheCondState.CondMet;
2101 }
2102
2103 return false;
2104}
2105
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002106/// ParseDirectiveElseIf
2107/// ::= .elseif expression
2108bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2109 if (TheCondState.TheCond != AsmCond::IfCond &&
2110 TheCondState.TheCond != AsmCond::ElseIfCond)
2111 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2112 " an .elseif");
2113 TheCondState.TheCond = AsmCond::ElseIfCond;
2114
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002115 bool LastIgnoreState = false;
2116 if (!TheCondStack.empty())
2117 LastIgnoreState = TheCondStack.back().Ignore;
2118 if (LastIgnoreState || TheCondState.CondMet) {
2119 TheCondState.Ignore = true;
2120 EatToEndOfStatement();
2121 }
2122 else {
2123 int64_t ExprValue;
2124 if (ParseAbsoluteExpression(ExprValue))
2125 return true;
2126
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002127 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002128 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002129
Sean Callanan79ed1a82010-01-19 20:22:31 +00002130 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002131 TheCondState.CondMet = ExprValue;
2132 TheCondState.Ignore = !TheCondState.CondMet;
2133 }
2134
2135 return false;
2136}
2137
2138/// ParseDirectiveElse
2139/// ::= .else
2140bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002141 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002142 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002143
Sean Callanan79ed1a82010-01-19 20:22:31 +00002144 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002145
2146 if (TheCondState.TheCond != AsmCond::IfCond &&
2147 TheCondState.TheCond != AsmCond::ElseIfCond)
2148 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2149 ".elseif");
2150 TheCondState.TheCond = AsmCond::ElseCond;
2151 bool LastIgnoreState = false;
2152 if (!TheCondStack.empty())
2153 LastIgnoreState = TheCondStack.back().Ignore;
2154 if (LastIgnoreState || TheCondState.CondMet)
2155 TheCondState.Ignore = true;
2156 else
2157 TheCondState.Ignore = false;
2158
2159 return false;
2160}
2161
2162/// ParseDirectiveEndIf
2163/// ::= .endif
2164bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002165 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002166 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002167
Sean Callanan79ed1a82010-01-19 20:22:31 +00002168 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002169
2170 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2171 TheCondStack.empty())
2172 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2173 ".else");
2174 if (!TheCondStack.empty()) {
2175 TheCondState = TheCondStack.back();
2176 TheCondStack.pop_back();
2177 }
2178
2179 return false;
2180}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002181
2182/// ParseDirectiveFile
2183/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002184bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002185 // FIXME: I'm not sure what this is.
2186 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002187 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002188 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002189 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002190 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002191
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002192 if (FileNumber < 1)
2193 return TokError("file number less than one");
2194 }
2195
Daniel Dunbareceec052010-07-12 17:45:27 +00002196 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002197 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002198
Chris Lattnerd32e8032010-01-25 19:02:58 +00002199 StringRef Filename = getTok().getString();
2200 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002201 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002202
Daniel Dunbareceec052010-07-12 17:45:27 +00002203 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002204 return TokError("unexpected token in '.file' directive");
2205
Chris Lattnerd32e8032010-01-25 19:02:58 +00002206 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002207 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002208 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002209 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002210 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002211 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002212
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002213 return false;
2214}
2215
2216/// ParseDirectiveLine
2217/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002218bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002219 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2220 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002221 return TokError("unexpected token in '.line' directive");
2222
Sean Callanan18b83232010-01-19 21:44:56 +00002223 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002224 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002225 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002226
2227 // FIXME: Do something with the .line.
2228 }
2229
Daniel Dunbareceec052010-07-12 17:45:27 +00002230 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002231 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002232
2233 return false;
2234}
2235
2236
2237/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002238/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002239/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2240/// The first number is a file number, must have been previously assigned with
2241/// a .file directive, the second number is the line number and optionally the
2242/// third number is a column position (zero if not specified). The remaining
2243/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002244bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002245
Daniel Dunbareceec052010-07-12 17:45:27 +00002246 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002247 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002248 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002249 if (FileNumber < 1)
2250 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002251 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002252 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002253 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002254
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002255 int64_t LineNumber = 0;
2256 if (getLexer().is(AsmToken::Integer)) {
2257 LineNumber = getTok().getIntVal();
2258 if (LineNumber < 1)
2259 return TokError("line number less than one in '.loc' directive");
2260 Lex();
2261 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002262
2263 int64_t ColumnPos = 0;
2264 if (getLexer().is(AsmToken::Integer)) {
2265 ColumnPos = getTok().getIntVal();
2266 if (ColumnPos < 0)
2267 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002268 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002269 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002270
Kevin Enderbyc0957932010-09-30 16:52:03 +00002271 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002272 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002273 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002274 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2275 for (;;) {
2276 if (getLexer().is(AsmToken::EndOfStatement))
2277 break;
2278
2279 StringRef Name;
2280 SMLoc Loc = getTok().getLoc();
2281 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002282 return TokError("unexpected token in '.loc' directive");
2283
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002284 if (Name == "basic_block")
2285 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2286 else if (Name == "prologue_end")
2287 Flags |= DWARF2_FLAG_PROLOGUE_END;
2288 else if (Name == "epilogue_begin")
2289 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2290 else if (Name == "is_stmt") {
2291 SMLoc Loc = getTok().getLoc();
2292 const MCExpr *Value;
2293 if (getParser().ParseExpression(Value))
2294 return true;
2295 // The expression must be the constant 0 or 1.
2296 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2297 int Value = MCE->getValue();
2298 if (Value == 0)
2299 Flags &= ~DWARF2_FLAG_IS_STMT;
2300 else if (Value == 1)
2301 Flags |= DWARF2_FLAG_IS_STMT;
2302 else
2303 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002304 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002305 else {
2306 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2307 }
2308 }
2309 else if (Name == "isa") {
2310 SMLoc Loc = getTok().getLoc();
2311 const MCExpr *Value;
2312 if (getParser().ParseExpression(Value))
2313 return true;
2314 // The expression must be a constant greater or equal to 0.
2315 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2316 int Value = MCE->getValue();
2317 if (Value < 0)
2318 return Error(Loc, "isa number less than zero");
2319 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002320 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002321 else {
2322 return Error(Loc, "isa number not a constant value");
2323 }
2324 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002325 else if (Name == "discriminator") {
2326 if (getParser().ParseAbsoluteExpression(Discriminator))
2327 return true;
2328 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002329 else {
2330 return Error(Loc, "unknown sub-directive in '.loc' directive");
2331 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002332
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002333 if (getLexer().is(AsmToken::EndOfStatement))
2334 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002335 }
2336 }
2337
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002338 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002339 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002340
2341 return false;
2342}
2343
Daniel Dunbar138abae2010-10-16 04:56:42 +00002344/// ParseDirectiveStabs
2345/// ::= .stabs string, number, number, number
2346bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2347 SMLoc DirectiveLoc) {
2348 return TokError("unsupported directive '" + Directive + "'");
2349}
2350
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002351/// ParseDirectiveCFISections
2352/// ::= .cfi_sections section [, section]
2353bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2354 SMLoc DirectiveLoc) {
2355 StringRef Name;
2356 bool EH = false;
2357 bool Debug = false;
2358
2359 if (getParser().ParseIdentifier(Name))
2360 return TokError("Expected an identifier");
2361
2362 if (Name == ".eh_frame")
2363 EH = true;
2364 else if (Name == ".debug_frame")
2365 Debug = true;
2366
2367 if (getLexer().is(AsmToken::Comma)) {
2368 Lex();
2369
2370 if (getParser().ParseIdentifier(Name))
2371 return TokError("Expected an identifier");
2372
2373 if (Name == ".eh_frame")
2374 EH = true;
2375 else if (Name == ".debug_frame")
2376 Debug = true;
2377 }
2378
2379 getStreamer().EmitCFISections(EH, Debug);
2380
2381 return false;
2382}
2383
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002384/// ParseDirectiveCFIStartProc
2385/// ::= .cfi_startproc
2386bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2387 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002388 getStreamer().EmitCFIStartProc();
2389 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002390}
2391
2392/// ParseDirectiveCFIEndProc
2393/// ::= .cfi_endproc
2394bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002395 getStreamer().EmitCFIEndProc();
2396 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002397}
2398
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002399/// ParseRegisterOrRegisterNumber - parse register name or number.
2400bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2401 SMLoc DirectiveLoc) {
2402 unsigned RegNo;
2403
Jim Grosbach6f888a82011-06-02 17:14:04 +00002404 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002405 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2406 DirectiveLoc))
2407 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002408 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002409 } else
2410 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002411
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002412 return false;
2413}
2414
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002415/// ParseDirectiveCFIDefCfa
2416/// ::= .cfi_def_cfa register, offset
2417bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2418 SMLoc DirectiveLoc) {
2419 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002420 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002421 return true;
2422
2423 if (getLexer().isNot(AsmToken::Comma))
2424 return TokError("unexpected token in directive");
2425 Lex();
2426
2427 int64_t Offset = 0;
2428 if (getParser().ParseAbsoluteExpression(Offset))
2429 return true;
2430
Rafael Espindola066c2f42011-04-12 23:59:07 +00002431 getStreamer().EmitCFIDefCfa(Register, Offset);
2432 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002433}
2434
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002435/// ParseDirectiveCFIDefCfaOffset
2436/// ::= .cfi_def_cfa_offset offset
2437bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2438 SMLoc DirectiveLoc) {
2439 int64_t Offset = 0;
2440 if (getParser().ParseAbsoluteExpression(Offset))
2441 return true;
2442
Rafael Espindola066c2f42011-04-12 23:59:07 +00002443 getStreamer().EmitCFIDefCfaOffset(Offset);
2444 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002445}
2446
2447/// ParseDirectiveCFIAdjustCfaOffset
2448/// ::= .cfi_adjust_cfa_offset adjustment
2449bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2450 SMLoc DirectiveLoc) {
2451 int64_t Adjustment = 0;
2452 if (getParser().ParseAbsoluteExpression(Adjustment))
2453 return true;
2454
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002455 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2456 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002457}
2458
2459/// ParseDirectiveCFIDefCfaRegister
2460/// ::= .cfi_def_cfa_register register
2461bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2462 SMLoc DirectiveLoc) {
2463 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002464 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002465 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002466
Rafael Espindola066c2f42011-04-12 23:59:07 +00002467 getStreamer().EmitCFIDefCfaRegister(Register);
2468 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002469}
2470
2471/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002472/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002473bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2474 int64_t Register = 0;
2475 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002476
2477 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002478 return true;
2479
2480 if (getLexer().isNot(AsmToken::Comma))
2481 return TokError("unexpected token in directive");
2482 Lex();
2483
2484 if (getParser().ParseAbsoluteExpression(Offset))
2485 return true;
2486
Rafael Espindola066c2f42011-04-12 23:59:07 +00002487 getStreamer().EmitCFIOffset(Register, Offset);
2488 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002489}
2490
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002491/// ParseDirectiveCFIRelOffset
2492/// ::= .cfi_rel_offset register, offset
2493bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2494 SMLoc DirectiveLoc) {
2495 int64_t Register = 0;
2496
2497 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2498 return true;
2499
2500 if (getLexer().isNot(AsmToken::Comma))
2501 return TokError("unexpected token in directive");
2502 Lex();
2503
2504 int64_t Offset = 0;
2505 if (getParser().ParseAbsoluteExpression(Offset))
2506 return true;
2507
Rafael Espindola25f492e2011-04-12 16:12:03 +00002508 getStreamer().EmitCFIRelOffset(Register, Offset);
2509 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002510}
2511
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002512static bool isValidEncoding(int64_t Encoding) {
2513 if (Encoding & ~0xff)
2514 return false;
2515
2516 if (Encoding == dwarf::DW_EH_PE_omit)
2517 return true;
2518
2519 const unsigned Format = Encoding & 0xf;
2520 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2521 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2522 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2523 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2524 return false;
2525
Rafael Espindolacaf11582010-12-29 04:31:26 +00002526 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002527 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002528 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002529 return false;
2530
2531 return true;
2532}
2533
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002534/// ParseDirectiveCFIPersonalityOrLsda
2535/// ::= .cfi_personality encoding, [symbol_name]
2536/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002537bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002538 SMLoc DirectiveLoc) {
2539 int64_t Encoding = 0;
2540 if (getParser().ParseAbsoluteExpression(Encoding))
2541 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002542 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002543 return false;
2544
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002545 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002546 return TokError("unsupported encoding.");
2547
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002548 if (getLexer().isNot(AsmToken::Comma))
2549 return TokError("unexpected token in directive");
2550 Lex();
2551
2552 StringRef Name;
2553 if (getParser().ParseIdentifier(Name))
2554 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002555
2556 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2557
2558 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002559 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002560 else {
2561 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002562 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002563 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002564 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002565}
2566
Rafael Espindolafe024d02010-12-28 18:36:23 +00002567/// ParseDirectiveCFIRememberState
2568/// ::= .cfi_remember_state
2569bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2570 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002571 getStreamer().EmitCFIRememberState();
2572 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002573}
2574
2575/// ParseDirectiveCFIRestoreState
2576/// ::= .cfi_remember_state
2577bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2578 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002579 getStreamer().EmitCFIRestoreState();
2580 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002581}
2582
Rafael Espindolac5754392011-04-12 15:31:05 +00002583/// ParseDirectiveCFISameValue
2584/// ::= .cfi_same_value register
2585bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2586 SMLoc DirectiveLoc) {
2587 int64_t Register = 0;
2588
2589 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2590 return true;
2591
2592 getStreamer().EmitCFISameValue(Register);
2593
2594 return false;
2595}
2596
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002597/// ParseDirectiveMacrosOnOff
2598/// ::= .macros_on
2599/// ::= .macros_off
2600bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2601 SMLoc DirectiveLoc) {
2602 if (getLexer().isNot(AsmToken::EndOfStatement))
2603 return Error(getLexer().getLoc(),
2604 "unexpected token in '" + Directive + "' directive");
2605
2606 getParser().MacrosEnabled = Directive == ".macros_on";
2607
2608 return false;
2609}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002610
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002611/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002612/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002613bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2614 SMLoc DirectiveLoc) {
2615 StringRef Name;
2616 if (getParser().ParseIdentifier(Name))
2617 return TokError("expected identifier in directive");
2618
Rafael Espindola65366442011-06-05 02:43:45 +00002619 std::vector<StringRef> Parameters;
2620 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2621 for(;;) {
2622 StringRef Parameter;
2623 if (getParser().ParseIdentifier(Parameter))
2624 return TokError("expected identifier in directive");
2625 Parameters.push_back(Parameter);
2626
2627 if (getLexer().isNot(AsmToken::Comma))
2628 break;
2629 Lex();
2630 }
2631 }
2632
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002633 if (getLexer().isNot(AsmToken::EndOfStatement))
2634 return TokError("unexpected token in '.macro' directive");
2635
2636 // Eat the end of statement.
2637 Lex();
2638
2639 AsmToken EndToken, StartToken = getTok();
2640
2641 // Lex the macro definition.
2642 for (;;) {
2643 // Check whether we have reached the end of the file.
2644 if (getLexer().is(AsmToken::Eof))
2645 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2646
2647 // Otherwise, check whether we have reach the .endmacro.
2648 if (getLexer().is(AsmToken::Identifier) &&
2649 (getTok().getIdentifier() == ".endm" ||
2650 getTok().getIdentifier() == ".endmacro")) {
2651 EndToken = getTok();
2652 Lex();
2653 if (getLexer().isNot(AsmToken::EndOfStatement))
2654 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2655 "' directive");
2656 break;
2657 }
2658
2659 // Otherwise, scan til the end of the statement.
2660 getParser().EatToEndOfStatement();
2661 }
2662
2663 if (getParser().MacroMap.lookup(Name)) {
2664 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2665 }
2666
2667 const char *BodyStart = StartToken.getLoc().getPointer();
2668 const char *BodyEnd = EndToken.getLoc().getPointer();
2669 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002670 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002671 return false;
2672}
2673
2674/// ParseDirectiveEndMacro
2675/// ::= .endm
2676/// ::= .endmacro
2677bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2678 SMLoc DirectiveLoc) {
2679 if (getLexer().isNot(AsmToken::EndOfStatement))
2680 return TokError("unexpected token in '" + Directive + "' directive");
2681
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002682 // If we are inside a macro instantiation, terminate the current
2683 // instantiation.
2684 if (!getParser().ActiveMacros.empty()) {
2685 getParser().HandleMacroExit();
2686 return false;
2687 }
2688
2689 // Otherwise, this .endmacro is a stray entry in the file; well formed
2690 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002691 return TokError("unexpected '" + Directive + "' in file, "
2692 "no current macro definition");
2693}
2694
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002695bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002696 getParser().CheckForValidSection();
2697
2698 const MCExpr *Value;
2699
2700 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002701 return true;
2702
2703 if (getLexer().isNot(AsmToken::EndOfStatement))
2704 return TokError("unexpected token in directive");
2705
2706 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002707 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002708 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002709 getStreamer().EmitULEB128Value(Value);
2710
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002711 return false;
2712}
2713
2714
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002715/// \brief Create an MCAsmParser instance.
2716MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2717 MCContext &C, MCStreamer &Out,
2718 const MCAsmInfo &MAI) {
2719 return new AsmParser(T, SM, C, Out, MAI);
2720}