blob: 9113dd7c3106b2ef13839ab3317bed9e962d38b7 [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"
Evan Cheng94b95502011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000023#include "llvm/MC/MCParser/AsmCond.h"
24#include "llvm/MC/MCParser/AsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000027#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000028#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000029#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000030#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000031#include "llvm/MC/MCTargetAsmParser.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:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000119 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000120 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();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000156 void EatToEndOfLine();
157 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000158
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000159 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000160 bool expandMacro(SmallString<256> &Buf, StringRef Body,
161 const std::vector<StringRef> &Parameters,
162 const std::vector<std::vector<AsmToken> > &A,
163 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000164 void HandleMacroExit();
165
166 void PrintMacroInstantiations();
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000167 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type,
168 bool ShowLine = true) const {
169 SrcMgr.PrintMessage(Loc, Msg, Type, ShowLine);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000170 }
171
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000172 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
173 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000174
175 /// \brief Reset the current lexer position to that given by \arg Loc. The
176 /// current token is not set; clients should ensure Lex() is called
177 /// subsequently.
178 void JumpToLoc(SMLoc Loc);
179
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000180 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000181
182 /// \brief Parse up to the end of statement and a return the contents from the
183 /// current token until the end of the statement; the current token on exit
184 /// will be either the EndOfStatement or EOF.
185 StringRef ParseStringToEndOfStatement();
186
Nico Weber4c4c7322011-01-28 03:04:41 +0000187 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000188
189 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
190 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
191 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000192 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000193
194 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
195 /// and set \arg Res to the identifier contents.
196 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000197
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000198 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000199
200 // ".ascii", ".asciiz", ".string"
201 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000202 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000203 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000204 bool ParseDirectiveFill(); // ".fill"
205 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000206 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000207 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000208 bool ParseDirectiveOrg(); // ".org"
209 // ".align{,32}", ".p2align{,w,l}"
210 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
211
212 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
213 /// accepts a single symbol (which should be a label or an external).
214 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000215
216 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
217
218 bool ParseDirectiveAbort(); // ".abort"
219 bool ParseDirectiveInclude(); // ".include"
220
221 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000222 // ".ifdef" or ".ifndef", depending on expect_defined
223 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000224 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
225 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
226 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
227
228 /// ParseEscapedString - Parse the current token as a string which may include
229 /// escaped characters and return the string contents.
230 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000231
232 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
233 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000234};
235
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000236/// \brief Generic implementations of directive handling, etc. which is shared
237/// (or the default, at least) for all assembler parser.
238class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000239 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
240 void AddDirectiveHandler(StringRef Directive) {
241 getParser().AddDirectiveHandler(this, Directive,
242 HandleDirective<GenericAsmParser, Handler>);
243 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000244public:
245 GenericAsmParser() {}
246
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000247 AsmParser &getParser() {
248 return (AsmParser&) this->MCAsmParserExtension::getParser();
249 }
250
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000251 virtual void Initialize(MCAsmParser &Parser) {
252 // Call the base implementation.
253 this->MCAsmParserExtension::Initialize(Parser);
254
255 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000256 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
257 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
258 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000259 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000260
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000261 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000262 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
263 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000264 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
265 ".cfi_startproc");
266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
267 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
269 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000270 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
271 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
273 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
275 ".cfi_def_cfa_register");
276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
277 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000278 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
279 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000280 AddDirectiveHandler<
281 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
282 AddDirectiveHandler<
283 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000284 AddDirectiveHandler<
285 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
286 AddDirectiveHandler<
287 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000288 AddDirectiveHandler<
289 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000290
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000291 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000292 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
293 ".macros_on");
294 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
295 ".macros_off");
296 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
297 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
298 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000299
300 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
301 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000302 }
303
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000304 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
305
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000306 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
307 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
308 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000309 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000310 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000311 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
312 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000313 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000314 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000315 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000316 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
317 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000318 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000319 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000320 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
321 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000322 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000323
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000324 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000325 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
326 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000327
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000328 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000329};
330
331}
332
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000333namespace llvm {
334
335extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000336extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000337extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000338
339}
340
Chris Lattneraaec2052010-01-19 19:46:13 +0000341enum { DEFAULT_ADDRSPACE = 0 };
342
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000343AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000344 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000345 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000346 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000347 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000348 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000349
350 // Initialize the generic parser.
351 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000352
353 // Initialize the platform / file format parser.
354 //
355 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
356 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000357 if (_MAI.hasMicrosoftFastStdCallMangling()) {
358 PlatformParser = createCOFFAsmParser();
359 PlatformParser->Initialize(*this);
360 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000361 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000362 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000363 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000364 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000365 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000366 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000367}
368
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000369AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000370 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
371
372 // Destroy any macros.
373 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
374 ie = MacroMap.end(); it != ie; ++it)
375 delete it->getValue();
376
Daniel Dunbare4749702010-07-12 18:12:02 +0000377 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000378 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000379}
380
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000381void AsmParser::PrintMacroInstantiations() {
382 // Print the active macro instantiation stack.
383 for (std::vector<MacroInstantiation*>::const_reverse_iterator
384 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
385 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
386 "note");
387}
388
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000389bool AsmParser::Warning(SMLoc L, const Twine &Msg) {
390 if (FatalAssemblerWarnings)
391 return Error(L, Msg);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000392 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000393 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000394 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000395}
396
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000397bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000398 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000399 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000400 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000401 return true;
402}
403
Sean Callananfd0b0282010-01-21 00:19:58 +0000404bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000405 std::string IncludedFile;
406 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000407 if (NewBuf == -1)
408 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000409
Sean Callananfd0b0282010-01-21 00:19:58 +0000410 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000411
Sean Callananfd0b0282010-01-21 00:19:58 +0000412 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000413
Sean Callananfd0b0282010-01-21 00:19:58 +0000414 return false;
415}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000416
417void AsmParser::JumpToLoc(SMLoc Loc) {
418 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
419 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
420}
421
Sean Callananfd0b0282010-01-21 00:19:58 +0000422const AsmToken &AsmParser::Lex() {
423 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000424
Sean Callananfd0b0282010-01-21 00:19:58 +0000425 if (tok->is(AsmToken::Eof)) {
426 // If this is the end of an included file, pop the parent file off the
427 // include stack.
428 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
429 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000430 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000431 tok = &Lexer.Lex();
432 }
433 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000434
Sean Callananfd0b0282010-01-21 00:19:58 +0000435 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000436 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000437
Sean Callananfd0b0282010-01-21 00:19:58 +0000438 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000439}
440
Chris Lattner79180e22010-04-05 23:15:42 +0000441bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000442 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000443 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000444 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000445
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000446 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000447 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000448
449 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000450 AsmCond StartingCondState = TheCondState;
451
Chris Lattnerb717fb02009-07-02 21:53:43 +0000452 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000453 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000454 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000455
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000456 // We had an error, validate that one was emitted and recover by skipping to
457 // the next line.
458 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000459 EatToEndOfStatement();
460 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000461
462 if (TheCondState.TheCond != StartingCondState.TheCond ||
463 TheCondState.Ignore != StartingCondState.Ignore)
464 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000465
466 // Check to see there are no empty DwarfFile slots.
467 const std::vector<MCDwarfFile *> &MCDwarfFiles =
468 getContext().getMCDwarfFiles();
469 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000470 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000471 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000472 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000473
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000474 // Check to see that all assembler local symbols were actually defined.
475 // Targets that don't do subsections via symbols may not want this, though,
476 // so conservatively exclude them. Only do this if we're finalizing, though,
477 // as otherwise we won't necessarilly have seen everything yet.
478 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
479 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
480 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
481 e = Symbols.end();
482 i != e; ++i) {
483 MCSymbol *Sym = i->getValue();
484 // Variable symbols may not be marked as defined, so check those
485 // explicitly. If we know it's a variable, we have a definition for
486 // the purposes of this check.
487 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
488 // FIXME: We would really like to refer back to where the symbol was
489 // first referenced for a source location. We need to add something
490 // to track that. Currently, we just point to the end of the file.
491 PrintMessage(getLexer().getLoc(), "assembler local symbol '" +
492 Sym->getName() + "' not defined", "error", false);
493 }
494 }
495
496
Chris Lattner79180e22010-04-05 23:15:42 +0000497 // Finalize the output stream if there are no errors and if the client wants
498 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000499 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000500 Out.Finish();
501
Chris Lattnerb717fb02009-07-02 21:53:43 +0000502 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000503}
504
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000505void AsmParser::CheckForValidSection() {
506 if (!getStreamer().getCurrentSection()) {
507 TokError("expected section directive before assembly directive");
508 Out.SwitchSection(Ctx.getMachOSection(
509 "__TEXT", "__text",
510 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
511 0, SectionKind::getText()));
512 }
513}
514
Chris Lattner2cf5f142009-06-22 01:29:09 +0000515/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
516void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000517 while (Lexer.isNot(AsmToken::EndOfStatement) &&
518 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000519 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000520
Chris Lattner2cf5f142009-06-22 01:29:09 +0000521 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000522 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000523 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000524}
525
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000526StringRef AsmParser::ParseStringToEndOfStatement() {
527 const char *Start = getTok().getLoc().getPointer();
528
529 while (Lexer.isNot(AsmToken::EndOfStatement) &&
530 Lexer.isNot(AsmToken::Eof))
531 Lex();
532
533 const char *End = getTok().getLoc().getPointer();
534 return StringRef(Start, End - Start);
535}
Chris Lattnerc4193832009-06-22 05:51:26 +0000536
Chris Lattner74ec1a32009-06-22 06:32:03 +0000537/// ParseParenExpr - Parse a paren expression and return it.
538/// NOTE: This assumes the leading '(' has already been consumed.
539///
540/// parenexpr ::= expr)
541///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000542bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000543 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000544 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000545 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000546 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000547 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000548 return false;
549}
Chris Lattnerc4193832009-06-22 05:51:26 +0000550
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000551/// ParseBracketExpr - Parse a bracket expression and return it.
552/// NOTE: This assumes the leading '[' has already been consumed.
553///
554/// bracketexpr ::= expr]
555///
556bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
557 if (ParseExpression(Res)) return true;
558 if (Lexer.isNot(AsmToken::RBrac))
559 return TokError("expected ']' in brackets expression");
560 EndLoc = Lexer.getLoc();
561 Lex();
562 return false;
563}
564
Chris Lattner74ec1a32009-06-22 06:32:03 +0000565/// ParsePrimaryExpr - Parse a primary expression and return it.
566/// primaryexpr ::= (parenexpr
567/// primaryexpr ::= symbol
568/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000569/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000570/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000571bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000572 switch (Lexer.getKind()) {
573 default:
574 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000575 // If we have an error assume that we've already handled it.
576 case AsmToken::Error:
577 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000578 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000579 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000580 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000581 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000582 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000583 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000584 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000585 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000586 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000587 EndLoc = Lexer.getLoc();
588
589 StringRef Identifier;
590 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000591 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000592
Daniel Dunbarfffff912009-10-16 01:34:54 +0000593 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000594 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000595 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000596
597 // Lookup the symbol variant if used.
598 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000599 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000600 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000601 if (Variant == MCSymbolRefExpr::VK_Invalid) {
602 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000603 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000604 }
605 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000606
Daniel Dunbarfffff912009-10-16 01:34:54 +0000607 // If this is an absolute variable reference, substitute it now to preserve
608 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000609 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000610 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000611 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000612
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000613 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000614 return false;
615 }
616
617 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000618 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000619 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000620 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000621 case AsmToken::Integer: {
622 SMLoc Loc = getTok().getLoc();
623 int64_t IntVal = getTok().getIntVal();
624 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000625 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000626 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000627 // Look for 'b' or 'f' following an Integer as a directional label
628 if (Lexer.getKind() == AsmToken::Identifier) {
629 StringRef IDVal = getTok().getString();
630 if (IDVal == "f" || IDVal == "b"){
631 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
632 IDVal == "f" ? 1 : 0);
633 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
634 getContext());
635 if(IDVal == "b" && Sym->isUndefined())
636 return Error(Loc, "invalid reference to undefined symbol");
637 EndLoc = Lexer.getLoc();
638 Lex(); // Eat identifier.
639 }
640 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000641 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000642 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000643 case AsmToken::Real: {
644 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000645 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000646 Res = MCConstantExpr::Create(IntVal, getContext());
647 Lex(); // Eat token.
648 return false;
649 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000650 case AsmToken::Dot: {
651 // This is a '.' reference, which references the current PC. Emit a
652 // temporary label to the streamer and refer to it.
653 MCSymbol *Sym = Ctx.CreateTempSymbol();
654 Out.EmitLabel(Sym);
655 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
656 EndLoc = Lexer.getLoc();
657 Lex(); // Eat identifier.
658 return false;
659 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000660 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000661 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000662 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000663 case AsmToken::LBrac:
664 if (!PlatformParser->HasBracketExpressions())
665 return TokError("brackets expression not supported on this target");
666 Lex(); // Eat the '['.
667 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000668 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000669 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000670 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000671 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000672 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000673 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000674 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000675 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000676 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000677 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000678 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000679 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000680 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000681 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000682 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000683 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000684 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000685 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000686 }
687}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000688
Chris Lattnerb4307b32010-01-15 19:28:38 +0000689bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000690 SMLoc EndLoc;
691 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000692}
693
Daniel Dunbarcceba832010-09-17 02:47:07 +0000694const MCExpr *
695AsmParser::ApplyModifierToExpr(const MCExpr *E,
696 MCSymbolRefExpr::VariantKind Variant) {
697 // Recurse over the given expression, rebuilding it to apply the given variant
698 // if there is exactly one symbol.
699 switch (E->getKind()) {
700 case MCExpr::Target:
701 case MCExpr::Constant:
702 return 0;
703
704 case MCExpr::SymbolRef: {
705 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
706
707 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
708 TokError("invalid variant on expression '" +
709 getTok().getIdentifier() + "' (already modified)");
710 return E;
711 }
712
713 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
714 }
715
716 case MCExpr::Unary: {
717 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
718 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
719 if (!Sub)
720 return 0;
721 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
722 }
723
724 case MCExpr::Binary: {
725 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
726 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
727 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
728
729 if (!LHS && !RHS)
730 return 0;
731
732 if (!LHS) LHS = BE->getLHS();
733 if (!RHS) RHS = BE->getRHS();
734
735 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
736 }
737 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000738
739 assert(0 && "Invalid expression kind!");
740 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000741}
742
Chris Lattner74ec1a32009-06-22 06:32:03 +0000743/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000744///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000745/// expr ::= expr &&,|| expr -> lowest.
746/// expr ::= expr |,^,&,! expr
747/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
748/// expr ::= expr <<,>> expr
749/// expr ::= expr +,- expr
750/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000751/// expr ::= primaryexpr
752///
Chris Lattner54482b42010-01-15 19:39:23 +0000753bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000754 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000755 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000756 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
757 return true;
758
Daniel Dunbarcceba832010-09-17 02:47:07 +0000759 // As a special case, we support 'a op b @ modifier' by rewriting the
760 // expression to include the modifier. This is inefficient, but in general we
761 // expect users to use 'a@modifier op b'.
762 if (Lexer.getKind() == AsmToken::At) {
763 Lex();
764
765 if (Lexer.isNot(AsmToken::Identifier))
766 return TokError("unexpected symbol modifier following '@'");
767
768 MCSymbolRefExpr::VariantKind Variant =
769 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
770 if (Variant == MCSymbolRefExpr::VK_Invalid)
771 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
772
773 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
774 if (!ModifiedRes) {
775 return TokError("invalid modifier '" + getTok().getIdentifier() +
776 "' (no symbols present)");
777 return true;
778 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000779
Daniel Dunbarcceba832010-09-17 02:47:07 +0000780 Res = ModifiedRes;
781 Lex();
782 }
783
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000784 // Try to constant fold it up front, if possible.
785 int64_t Value;
786 if (Res->EvaluateAsAbsolute(Value))
787 Res = MCConstantExpr::Create(Value, getContext());
788
789 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000790}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000791
Chris Lattnerb4307b32010-01-15 19:28:38 +0000792bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000793 Res = 0;
794 return ParseParenExpr(Res, EndLoc) ||
795 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000796}
797
Daniel Dunbar475839e2009-06-29 20:37:27 +0000798bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000799 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000800
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000801 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000802 if (ParseExpression(Expr))
803 return true;
804
Daniel Dunbare00b0112009-10-16 01:57:52 +0000805 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000806 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000807
808 return false;
809}
810
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000811static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000812 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000813 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000814 default:
815 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000816
Jim Grosbachfbe16812011-08-20 16:24:13 +0000817 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000818 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000819 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000820 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000821 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000822 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000823 return 1;
824
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000825
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000826 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000827 //
828 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000829 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000830 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000831 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000832 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000833 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000834 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000835 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000836 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000837 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000838
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000839 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000840 case AsmToken::EqualEqual:
841 Kind = MCBinaryExpr::EQ;
842 return 3;
843 case AsmToken::ExclaimEqual:
844 case AsmToken::LessGreater:
845 Kind = MCBinaryExpr::NE;
846 return 3;
847 case AsmToken::Less:
848 Kind = MCBinaryExpr::LT;
849 return 3;
850 case AsmToken::LessEqual:
851 Kind = MCBinaryExpr::LTE;
852 return 3;
853 case AsmToken::Greater:
854 Kind = MCBinaryExpr::GT;
855 return 3;
856 case AsmToken::GreaterEqual:
857 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000858 return 3;
859
Jim Grosbachfbe16812011-08-20 16:24:13 +0000860 // Intermediate Precedence: <<, >>
861 case AsmToken::LessLess:
862 Kind = MCBinaryExpr::Shl;
863 return 4;
864 case AsmToken::GreaterGreater:
865 Kind = MCBinaryExpr::Shr;
866 return 4;
867
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000868 // High Intermediate Precedence: +, -
869 case AsmToken::Plus:
870 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000871 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000872 case AsmToken::Minus:
873 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000874 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000875
Jim Grosbachfbe16812011-08-20 16:24:13 +0000876 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000877 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000878 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000879 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000880 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000881 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000882 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000883 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000884 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000885 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000886 }
887}
888
889
890/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
891/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000892bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
893 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000894 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000895 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000896 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000897
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000898 // If the next token is lower precedence than we are allowed to eat, return
899 // successfully with what we ate already.
900 if (TokPrec < Precedence)
901 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000902
Sean Callanan79ed1a82010-01-19 20:22:31 +0000903 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000904
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000905 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000906 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000907 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000908
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000909 // If BinOp binds less tightly with RHS than the operator after RHS, let
910 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000911 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000912 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000913 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000914 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000915 }
916
Daniel Dunbar475839e2009-06-29 20:37:27 +0000917 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000918 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000919 }
920}
921
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000922
923
924
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000925/// ParseStatement:
926/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000927/// ::= Label* Directive ...Operands... EndOfStatement
928/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000929bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000930 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000931 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000932 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000933 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000934 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000935
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000936 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000937 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000938 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000939 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000940 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000941 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000942 if (Lexer.is(AsmToken::Hash))
943 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000944
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000945 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000946 if (Lexer.is(AsmToken::Integer)) {
947 LocalLabelVal = getTok().getIntVal();
948 if (LocalLabelVal < 0) {
949 if (!TheCondState.Ignore)
950 return TokError("unexpected token at start of statement");
951 IDVal = "";
952 }
953 else {
954 IDVal = getTok().getString();
955 Lex(); // Consume the integer token to be used as an identifier token.
956 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000957 if (!TheCondState.Ignore)
958 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000959 }
960 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000961
962 } else if (Lexer.is(AsmToken::Dot)) {
963 // Treat '.' as a valid identifier in this context.
964 Lex();
965 IDVal = ".";
966
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000967 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000968 if (!TheCondState.Ignore)
969 return TokError("unexpected token at start of statement");
970 IDVal = "";
971 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000972
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000973
Chris Lattner7834fac2010-04-17 18:14:27 +0000974 // Handle conditional assembly here before checking for skipping. We
975 // have to do this so that .endif isn't skipped in a ".if 0" block for
976 // example.
977 if (IDVal == ".if")
978 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000979 if (IDVal == ".ifdef")
980 return ParseDirectiveIfdef(IDLoc, true);
981 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
982 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000983 if (IDVal == ".elseif")
984 return ParseDirectiveElseIf(IDLoc);
985 if (IDVal == ".else")
986 return ParseDirectiveElse(IDLoc);
987 if (IDVal == ".endif")
988 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000989
Chris Lattner7834fac2010-04-17 18:14:27 +0000990 // If we are in a ".if 0" block, ignore this statement.
991 if (TheCondState.Ignore) {
992 EatToEndOfStatement();
993 return false;
994 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000995
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000996 // FIXME: Recurse on local labels?
997
998 // See what kind of statement we have.
999 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001000 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001001 CheckForValidSection();
1002
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001003 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001004 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001005
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001006 // Diagnose attempt to use '.' as a label.
1007 if (IDVal == ".")
1008 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1009
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001010 // Diagnose attempt to use a variable as a label.
1011 //
1012 // FIXME: Diagnostics. Note the location of the definition as a label.
1013 // FIXME: This doesn't diagnose assignment to a symbol which has been
1014 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001015 MCSymbol *Sym;
1016 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001017 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001018 else
1019 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001020 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001021 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001022
Daniel Dunbar959fd882009-08-26 22:13:22 +00001023 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001024 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001025
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001026 // Consume any end of statement token, if present, to avoid spurious
1027 // AddBlankLine calls().
1028 if (Lexer.is(AsmToken::EndOfStatement)) {
1029 Lex();
1030 if (Lexer.is(AsmToken::Eof))
1031 return false;
1032 }
1033
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001034 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001035 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001036
Daniel Dunbar3f872332009-07-28 16:08:33 +00001037 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001038 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001039 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001040
Nico Weber4c4c7322011-01-28 03:04:41 +00001041 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001042
1043 default: // Normal instruction or directive.
1044 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001045 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001046
1047 // If macros are enabled, check to see if this is a macro instantiation.
1048 if (MacrosEnabled)
1049 if (const Macro *M = MacroMap.lookup(IDVal))
1050 return HandleMacroEntry(IDVal, IDLoc, M);
1051
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001052 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001053 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001054 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001055 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001056 return ParseDirectiveSet(IDVal, true);
1057 if (IDVal == ".equiv")
1058 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001059
Daniel Dunbara0d14262009-06-24 23:30:00 +00001060 // Data directives
1061
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001062 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001063 return ParseDirectiveAscii(IDVal, false);
1064 if (IDVal == ".asciz" || IDVal == ".string")
1065 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001066
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001067 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001068 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001069 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001070 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001071 if (IDVal == ".value")
1072 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001073 if (IDVal == ".2byte")
1074 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001075 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001076 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001077 if (IDVal == ".int")
1078 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001079 if (IDVal == ".4byte")
1080 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001081 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001082 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001083 if (IDVal == ".8byte")
1084 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001085 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001086 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1087 if (IDVal == ".double")
1088 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001089
Eli Friedman5d68ec22010-07-19 04:17:25 +00001090 if (IDVal == ".align") {
1091 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1092 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1093 }
1094 if (IDVal == ".align32") {
1095 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1096 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1097 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001098 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001099 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001100 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001101 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001102 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001103 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001104 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001105 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001106 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001107 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001108 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001109 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1110
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001111 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001112 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001113
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001114 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001115 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001116 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001117 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001118 if (IDVal == ".zero")
1119 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001120
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001121 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001122
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001123 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001124 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001125 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001126 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001127 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001128 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001129 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001130 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001131 if (IDVal == ".symbol_resolver")
1132 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001133 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001134 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001135 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001136 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001137 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001138 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001139 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001140 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001141 if (IDVal == ".weak_def_can_be_hidden")
1142 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001143
Hans Wennborg5cc64912011-06-18 13:51:54 +00001144 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001145 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001146 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001147 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001148
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001149 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001150 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001151 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001152 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001153
Evan Chengbd27f5a2011-07-27 00:38:12 +00001154 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001155 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001156
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001157 // Look up the handler in the handler table.
1158 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1159 DirectiveMap.lookup(IDVal);
1160 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001161 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001162
Kevin Enderby9c656452009-09-10 20:51:44 +00001163 // Target hook for parsing target specific directives.
1164 if (!getTargetParser().ParseDirective(ID))
1165 return false;
1166
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001167 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001168 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001169 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001170 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001171
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001172 CheckForValidSection();
1173
Chris Lattnera7f13542010-05-19 23:34:33 +00001174 // Canonicalize the opcode to lower case.
1175 SmallString<128> Opcode;
1176 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1177 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001178
Chris Lattner98986712010-01-14 22:21:20 +00001179 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001180 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001181 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001182
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001183 // Dump the parsed representation, if requested.
1184 if (getShowParsedOperands()) {
1185 SmallString<256> Str;
1186 raw_svector_ostream OS(Str);
1187 OS << "parsed instruction: [";
1188 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1189 if (i != 0)
1190 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001191 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001192 }
1193 OS << "]";
1194
1195 PrintMessage(IDLoc, OS.str(), "note");
1196 }
1197
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001198 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001199 if (!HadError)
1200 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1201 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001202
Chris Lattner98986712010-01-14 22:21:20 +00001203 // Free any parsed operands.
1204 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1205 delete ParsedOperands[i];
1206
Chris Lattnercbf8a982010-09-11 16:18:25 +00001207 // Don't skip the rest of the line, the instruction parser is responsible for
1208 // that.
1209 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001210}
Chris Lattner9a023f72009-06-24 04:43:34 +00001211
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001212/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1213/// since they may not be able to be tokenized to get to the end of line token.
1214void AsmParser::EatToEndOfLine() {
1215 Lexer.LexUntilEndOfLine();
1216 // Eat EOL.
1217 Lex();
1218}
1219
1220/// ParseCppHashLineFilenameComment as this:
1221/// ::= # number "filename"
1222/// or just as a full line comment if it doesn't have a number and a string.
1223bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1224 Lex(); // Eat the hash token.
1225
1226 if (getLexer().isNot(AsmToken::Integer)) {
1227 // Consume the line since in cases it is not a well-formed line directive,
1228 // as if were simply a full line comment.
1229 EatToEndOfLine();
1230 return false;
1231 }
1232
1233 int64_t LineNumber = getTok().getIntVal();
1234 // FIXME: remember to remove this line that is silencing a warning for now.
1235 (void) LineNumber;
1236 Lex();
1237
1238 if (getLexer().isNot(AsmToken::String)) {
1239 EatToEndOfLine();
1240 return false;
1241 }
1242
1243 StringRef Filename = getTok().getString();
1244 // Get rid of the enclosing quotes.
1245 Filename = Filename.substr(1, Filename.size()-2);
1246
1247 // TODO: Now with the Filename, LineNumber set up a mapping to the SMLoc for
1248 // later use by diagnostics.
1249
1250 // Ignore any trailing characters, they're just comment.
1251 EatToEndOfLine();
1252 return false;
1253}
1254
Rafael Espindola65366442011-06-05 02:43:45 +00001255bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1256 const std::vector<StringRef> &Parameters,
1257 const std::vector<std::vector<AsmToken> > &A,
1258 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001259 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001260 unsigned NParameters = Parameters.size();
1261 if (NParameters != 0 && NParameters != A.size())
1262 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001263
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001264 while (!Body.empty()) {
1265 // Scan for the next substitution.
1266 std::size_t End = Body.size(), Pos = 0;
1267 for (; Pos != End; ++Pos) {
1268 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001269 if (!NParameters) {
1270 // This macro has no parameters, look for $0, $1, etc.
1271 if (Body[Pos] != '$' || Pos + 1 == End)
1272 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001273
Rafael Espindola65366442011-06-05 02:43:45 +00001274 char Next = Body[Pos + 1];
1275 if (Next == '$' || Next == 'n' || isdigit(Next))
1276 break;
1277 } else {
1278 // This macro has parameters, look for \foo, \bar, etc.
1279 if (Body[Pos] == '\\' && Pos + 1 != End)
1280 break;
1281 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001282 }
1283
1284 // Add the prefix.
1285 OS << Body.slice(0, Pos);
1286
1287 // Check if we reached the end.
1288 if (Pos == End)
1289 break;
1290
Rafael Espindola65366442011-06-05 02:43:45 +00001291 if (!NParameters) {
1292 switch (Body[Pos+1]) {
1293 // $$ => $
1294 case '$':
1295 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001296 break;
1297
Rafael Espindola65366442011-06-05 02:43:45 +00001298 // $n => number of arguments
1299 case 'n':
1300 OS << A.size();
1301 break;
1302
1303 // $[0-9] => argument
1304 default: {
1305 // Missing arguments are ignored.
1306 unsigned Index = Body[Pos+1] - '0';
1307 if (Index >= A.size())
1308 break;
1309
1310 // Otherwise substitute with the token values, with spaces eliminated.
1311 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1312 ie = A[Index].end(); it != ie; ++it)
1313 OS << it->getString();
1314 break;
1315 }
1316 }
1317 Pos += 2;
1318 } else {
1319 unsigned I = Pos + 1;
1320 while (isalnum(Body[I]) && I + 1 != End)
1321 ++I;
1322
1323 const char *Begin = Body.data() + Pos +1;
1324 StringRef Argument(Begin, I - (Pos +1));
1325 unsigned Index = 0;
1326 for (; Index < NParameters; ++Index)
1327 if (Parameters[Index] == Argument)
1328 break;
1329
1330 // FIXME: We should error at the macro definition.
1331 if (Index == NParameters)
1332 return Error(L, "Parameter not found");
1333
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001334 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1335 ie = A[Index].end(); it != ie; ++it)
1336 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001337
Rafael Espindola65366442011-06-05 02:43:45 +00001338 Pos += 1 + Argument.size();
1339 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001340 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001341 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001342 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001343
1344 // We include the .endmacro in the buffer as our queue to exit the macro
1345 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001346 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001347 return false;
1348}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001349
Rafael Espindola65366442011-06-05 02:43:45 +00001350MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1351 MemoryBuffer *I)
1352 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1353{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001354}
1355
1356bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1357 const Macro *M) {
1358 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1359 // this, although we should protect against infinite loops.
1360 if (ActiveMacros.size() == 20)
1361 return TokError("macros cannot be nested more than 20 levels deep");
1362
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001363 // Parse the macro instantiation arguments.
1364 std::vector<std::vector<AsmToken> > MacroArguments;
1365 MacroArguments.push_back(std::vector<AsmToken>());
1366 unsigned ParenLevel = 0;
1367 for (;;) {
1368 if (Lexer.is(AsmToken::Eof))
1369 return TokError("unexpected token in macro instantiation");
1370 if (Lexer.is(AsmToken::EndOfStatement))
1371 break;
1372
1373 // If we aren't inside parentheses and this is a comma, start a new token
1374 // list.
1375 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1376 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001377 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001378 // Adjust the current parentheses level.
1379 if (Lexer.is(AsmToken::LParen))
1380 ++ParenLevel;
1381 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1382 --ParenLevel;
1383
1384 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001385 MacroArguments.back().push_back(getTok());
1386 }
1387 Lex();
1388 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001389
Rafael Espindola65366442011-06-05 02:43:45 +00001390 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1391 // to hold the macro body with substitutions.
1392 SmallString<256> Buf;
1393 StringRef Body = M->Body;
1394
1395 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1396 return true;
1397
1398 MemoryBuffer *Instantiation =
1399 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1400
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001401 // Create the macro instantiation object and add to the current macro
1402 // instantiation stack.
1403 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001404 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001405 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001406 ActiveMacros.push_back(MI);
1407
1408 // Jump to the macro instantiation and prime the lexer.
1409 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1410 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1411 Lex();
1412
1413 return false;
1414}
1415
1416void AsmParser::HandleMacroExit() {
1417 // Jump to the EndOfStatement we should return to, and consume it.
1418 JumpToLoc(ActiveMacros.back()->ExitLoc);
1419 Lex();
1420
1421 // Pop the instantiation entry.
1422 delete ActiveMacros.back();
1423 ActiveMacros.pop_back();
1424}
1425
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001426static void MarkUsed(const MCExpr *Value) {
1427 switch (Value->getKind()) {
1428 case MCExpr::Binary:
1429 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1430 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1431 break;
1432 case MCExpr::Target:
1433 case MCExpr::Constant:
1434 break;
1435 case MCExpr::SymbolRef: {
1436 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1437 break;
1438 }
1439 case MCExpr::Unary:
1440 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1441 break;
1442 }
1443}
1444
Nico Weber4c4c7322011-01-28 03:04:41 +00001445bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001446 // FIXME: Use better location, we should use proper tokens.
1447 SMLoc EqualLoc = Lexer.getLoc();
1448
Daniel Dunbar821e3332009-08-31 08:09:28 +00001449 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001450 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001451 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001452
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001453 MarkUsed(Value);
1454
Daniel Dunbar3f872332009-07-28 16:08:33 +00001455 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001456 return TokError("unexpected token in assignment");
1457
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001458 // Error on assignment to '.'.
1459 if (Name == ".") {
1460 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1461 "(use '.space' or '.org').)"));
1462 }
1463
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001464 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001465 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001466
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001467 // Validate that the LHS is allowed to be a variable (either it has not been
1468 // used as a symbol, or it is an absolute symbol).
1469 MCSymbol *Sym = getContext().LookupSymbol(Name);
1470 if (Sym) {
1471 // Diagnose assignment to a label.
1472 //
1473 // FIXME: Diagnostics. Note the location of the definition as a label.
1474 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001475 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001476 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001477 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001478 return Error(EqualLoc, "redefinition of '" + Name + "'");
1479 else if (!Sym->isVariable())
1480 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001481 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001482 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1483 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001484
1485 // Don't count these checks as uses.
1486 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001487 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001488 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001489
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001490 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001491
1492 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001493 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001494
1495 return false;
1496}
1497
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001498/// ParseIdentifier:
1499/// ::= identifier
1500/// ::= string
1501bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001502 // The assembler has relaxed rules for accepting identifiers, in particular we
1503 // allow things like '.globl $foo', which would normally be separate
1504 // tokens. At this level, we have already lexed so we cannot (currently)
1505 // handle this as a context dependent token, instead we detect adjacent tokens
1506 // and return the combined identifier.
1507 if (Lexer.is(AsmToken::Dollar)) {
1508 SMLoc DollarLoc = getLexer().getLoc();
1509
1510 // Consume the dollar sign, and check for a following identifier.
1511 Lex();
1512 if (Lexer.isNot(AsmToken::Identifier))
1513 return true;
1514
1515 // We have a '$' followed by an identifier, make sure they are adjacent.
1516 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1517 return true;
1518
1519 // Construct the joined identifier and consume the token.
1520 Res = StringRef(DollarLoc.getPointer(),
1521 getTok().getIdentifier().size() + 1);
1522 Lex();
1523 return false;
1524 }
1525
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001526 if (Lexer.isNot(AsmToken::Identifier) &&
1527 Lexer.isNot(AsmToken::String))
1528 return true;
1529
Sean Callanan18b83232010-01-19 21:44:56 +00001530 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001531
Sean Callanan79ed1a82010-01-19 20:22:31 +00001532 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001533
1534 return false;
1535}
1536
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001537/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001538/// ::= .equ identifier ',' expression
1539/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001540/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001541bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001542 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001543
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001544 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001545 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001546
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001547 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001548 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001549 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001550
Nico Weber4c4c7322011-01-28 03:04:41 +00001551 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001552}
1553
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001554bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001555 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001556
1557 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001558 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001559 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1560 if (Str[i] != '\\') {
1561 Data += Str[i];
1562 continue;
1563 }
1564
1565 // Recognize escaped characters. Note that this escape semantics currently
1566 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1567 ++i;
1568 if (i == e)
1569 return TokError("unexpected backslash at end of string");
1570
1571 // Recognize octal sequences.
1572 if ((unsigned) (Str[i] - '0') <= 7) {
1573 // Consume up to three octal characters.
1574 unsigned Value = Str[i] - '0';
1575
1576 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1577 ++i;
1578 Value = Value * 8 + (Str[i] - '0');
1579
1580 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1581 ++i;
1582 Value = Value * 8 + (Str[i] - '0');
1583 }
1584 }
1585
1586 if (Value > 255)
1587 return TokError("invalid octal escape sequence (out of range)");
1588
1589 Data += (unsigned char) Value;
1590 continue;
1591 }
1592
1593 // Otherwise recognize individual escapes.
1594 switch (Str[i]) {
1595 default:
1596 // Just reject invalid escape sequences for now.
1597 return TokError("invalid escape sequence (unrecognized character)");
1598
1599 case 'b': Data += '\b'; break;
1600 case 'f': Data += '\f'; break;
1601 case 'n': Data += '\n'; break;
1602 case 'r': Data += '\r'; break;
1603 case 't': Data += '\t'; break;
1604 case '"': Data += '"'; break;
1605 case '\\': Data += '\\'; break;
1606 }
1607 }
1608
1609 return false;
1610}
1611
Daniel Dunbara0d14262009-06-24 23:30:00 +00001612/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001613/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1614bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001615 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001616 CheckForValidSection();
1617
Daniel Dunbara0d14262009-06-24 23:30:00 +00001618 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001619 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001620 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001621
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001622 std::string Data;
1623 if (ParseEscapedString(Data))
1624 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001625
1626 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001627 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001628 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1629
Sean Callanan79ed1a82010-01-19 20:22:31 +00001630 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001631
1632 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001633 break;
1634
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001635 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001636 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001637 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001638 }
1639 }
1640
Sean Callanan79ed1a82010-01-19 20:22:31 +00001641 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001642 return false;
1643}
1644
1645/// ParseDirectiveValue
1646/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1647bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001648 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001649 CheckForValidSection();
1650
Daniel Dunbara0d14262009-06-24 23:30:00 +00001651 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001652 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001653 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001654 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001655 return true;
1656
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001657 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001658 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1659 assert(Size <= 8 && "Invalid size");
1660 uint64_t IntValue = MCE->getValue();
1661 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1662 return Error(ExprLoc, "literal value out of range for directive");
1663 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1664 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001665 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001666
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001667 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001668 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001669
Daniel Dunbara0d14262009-06-24 23:30:00 +00001670 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001671 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001672 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001673 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001674 }
1675 }
1676
Sean Callanan79ed1a82010-01-19 20:22:31 +00001677 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001678 return false;
1679}
1680
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001681/// ParseDirectiveRealValue
1682/// ::= (.single | .double) [ expression (, expression)* ]
1683bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1684 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1685 CheckForValidSection();
1686
1687 for (;;) {
1688 // We don't truly support arithmetic on floating point expressions, so we
1689 // have to manually parse unary prefixes.
1690 bool IsNeg = false;
1691 if (getLexer().is(AsmToken::Minus)) {
1692 Lex();
1693 IsNeg = true;
1694 } else if (getLexer().is(AsmToken::Plus))
1695 Lex();
1696
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001697 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001698 getLexer().isNot(AsmToken::Real) &&
1699 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001700 return TokError("unexpected token in directive");
1701
1702 // Convert to an APFloat.
1703 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001704 StringRef IDVal = getTok().getString();
1705 if (getLexer().is(AsmToken::Identifier)) {
1706 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1707 Value = APFloat::getInf(Semantics);
1708 else if (!IDVal.compare_lower("nan"))
1709 Value = APFloat::getNaN(Semantics, false, ~0);
1710 else
1711 return TokError("invalid floating point literal");
1712 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001713 APFloat::opInvalidOp)
1714 return TokError("invalid floating point literal");
1715 if (IsNeg)
1716 Value.changeSign();
1717
1718 // Consume the numeric token.
1719 Lex();
1720
1721 // Emit the value as an integer.
1722 APInt AsInt = Value.bitcastToAPInt();
1723 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1724 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1725
1726 if (getLexer().is(AsmToken::EndOfStatement))
1727 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001728
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001729 if (getLexer().isNot(AsmToken::Comma))
1730 return TokError("unexpected token in directive");
1731 Lex();
1732 }
1733 }
1734
1735 Lex();
1736 return false;
1737}
1738
Daniel Dunbara0d14262009-06-24 23:30:00 +00001739/// ParseDirectiveSpace
1740/// ::= .space expression [ , expression ]
1741bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001742 CheckForValidSection();
1743
Daniel Dunbara0d14262009-06-24 23:30:00 +00001744 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001745 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001746 return true;
1747
1748 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001749 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1750 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001751 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001752 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001753
Daniel Dunbar475839e2009-06-29 20:37:27 +00001754 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001755 return true;
1756
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001757 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001758 return TokError("unexpected token in '.space' directive");
1759 }
1760
Sean Callanan79ed1a82010-01-19 20:22:31 +00001761 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001762
1763 if (NumBytes <= 0)
1764 return TokError("invalid number of bytes in '.space' directive");
1765
1766 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001767 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001768
1769 return false;
1770}
1771
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001772/// ParseDirectiveZero
1773/// ::= .zero expression
1774bool AsmParser::ParseDirectiveZero() {
1775 CheckForValidSection();
1776
1777 int64_t NumBytes;
1778 if (ParseAbsoluteExpression(NumBytes))
1779 return true;
1780
Rafael Espindolae452b172010-10-05 19:42:57 +00001781 int64_t Val = 0;
1782 if (getLexer().is(AsmToken::Comma)) {
1783 Lex();
1784 if (ParseAbsoluteExpression(Val))
1785 return true;
1786 }
1787
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001788 if (getLexer().isNot(AsmToken::EndOfStatement))
1789 return TokError("unexpected token in '.zero' directive");
1790
1791 Lex();
1792
Rafael Espindolae452b172010-10-05 19:42:57 +00001793 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001794
1795 return false;
1796}
1797
Daniel Dunbara0d14262009-06-24 23:30:00 +00001798/// ParseDirectiveFill
1799/// ::= .fill expression , expression , expression
1800bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001801 CheckForValidSection();
1802
Daniel Dunbara0d14262009-06-24 23:30:00 +00001803 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001804 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001805 return true;
1806
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001807 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001808 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001809 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001810
Daniel Dunbara0d14262009-06-24 23:30:00 +00001811 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001812 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001813 return true;
1814
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001815 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001816 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001817 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001818
Daniel Dunbara0d14262009-06-24 23:30:00 +00001819 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001820 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001821 return true;
1822
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001823 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001824 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001825
Sean Callanan79ed1a82010-01-19 20:22:31 +00001826 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001827
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001828 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1829 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001830
1831 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001832 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001833
1834 return false;
1835}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001836
1837/// ParseDirectiveOrg
1838/// ::= .org expression [ , expression ]
1839bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001840 CheckForValidSection();
1841
Daniel Dunbar821e3332009-08-31 08:09:28 +00001842 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001843 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001844 return true;
1845
1846 // Parse optional fill expression.
1847 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001848 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1849 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001850 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001851 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001852
Daniel Dunbar475839e2009-06-29 20:37:27 +00001853 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001854 return true;
1855
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001856 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001857 return TokError("unexpected token in '.org' directive");
1858 }
1859
Sean Callanan79ed1a82010-01-19 20:22:31 +00001860 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001861
1862 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1863 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001864 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001865
1866 return false;
1867}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001868
1869/// ParseDirectiveAlign
1870/// ::= {.align, ...} expression [ , expression [ , expression ]]
1871bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001872 CheckForValidSection();
1873
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001874 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001875 int64_t Alignment;
1876 if (ParseAbsoluteExpression(Alignment))
1877 return true;
1878
1879 SMLoc MaxBytesLoc;
1880 bool HasFillExpr = false;
1881 int64_t FillExpr = 0;
1882 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001883 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1884 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001885 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001886 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001887
1888 // The fill expression can be omitted while specifying a maximum number of
1889 // alignment bytes, e.g:
1890 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001891 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001892 HasFillExpr = true;
1893 if (ParseAbsoluteExpression(FillExpr))
1894 return true;
1895 }
1896
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001897 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1898 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001899 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001900 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001901
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001902 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001903 if (ParseAbsoluteExpression(MaxBytesToFill))
1904 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001905
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001906 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001907 return TokError("unexpected token in directive");
1908 }
1909 }
1910
Sean Callanan79ed1a82010-01-19 20:22:31 +00001911 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001912
Daniel Dunbar648ac512010-05-17 21:54:30 +00001913 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001914 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001915
1916 // Compute alignment in bytes.
1917 if (IsPow2) {
1918 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001919 if (Alignment >= 32) {
1920 Error(AlignmentLoc, "invalid alignment value");
1921 Alignment = 31;
1922 }
1923
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001924 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001925 }
1926
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001927 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001928 if (MaxBytesLoc.isValid()) {
1929 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001930 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1931 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001932 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001933 }
1934
1935 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001936 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1937 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001938 MaxBytesToFill = 0;
1939 }
1940 }
1941
Daniel Dunbar648ac512010-05-17 21:54:30 +00001942 // Check whether we should use optimal code alignment for this .align
1943 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001944 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001945 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1946 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001947 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001948 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001949 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001950 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1951 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001952 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001953
1954 return false;
1955}
1956
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001957/// ParseDirectiveSymbolAttribute
1958/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001959bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001960 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001961 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001962 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00001963 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001964
1965 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00001966 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001967
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001968 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001969
Jim Grosbach10ec6502011-09-15 17:56:49 +00001970 // Assembler local symbols don't make any sense here. Complain loudly.
1971 if (Sym->isTemporary())
1972 return Error(Loc, "non-local symbol required in directive");
1973
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001974 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001975
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001976 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001977 break;
1978
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001979 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001980 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001981 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001982 }
1983 }
1984
Sean Callanan79ed1a82010-01-19 20:22:31 +00001985 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001986 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001987}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001988
1989/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001990/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1991bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001992 CheckForValidSection();
1993
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001994 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001995 StringRef Name;
1996 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001997 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001998
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001999 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002000 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002001
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002002 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002003 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002004 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002005
2006 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002007 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002008 if (ParseAbsoluteExpression(Size))
2009 return true;
2010
2011 int64_t Pow2Alignment = 0;
2012 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002013 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002014 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002015 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002016 if (ParseAbsoluteExpression(Pow2Alignment))
2017 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002018
Chris Lattner258281d2010-01-19 06:22:22 +00002019 // If this target takes alignments in bytes (not log) validate and convert.
2020 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2021 if (!isPowerOf2_64(Pow2Alignment))
2022 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2023 Pow2Alignment = Log2_64(Pow2Alignment);
2024 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002025 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002026
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002027 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002028 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002029
Sean Callanan79ed1a82010-01-19 20:22:31 +00002030 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002031
Chris Lattner1fc3d752009-07-09 17:25:12 +00002032 // NOTE: a size of zero for a .comm should create a undefined symbol
2033 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002034 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002035 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2036 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002037
Eric Christopherc260a3e2010-05-14 01:38:54 +00002038 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002039 // may internally end up wanting an alignment in bytes.
2040 // FIXME: Diagnose overflow.
2041 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002042 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2043 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002044
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002045 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002046 return Error(IDLoc, "invalid symbol redefinition");
2047
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002048 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002049 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002050 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002051 getStreamer().EmitZerofill(Ctx.getMachOSection(
2052 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2053 0, SectionKind::getBSS()),
2054 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002055 return false;
2056 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002057
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002058 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002059 return false;
2060}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002061
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002062/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002063/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002064bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002065 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002066 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002067
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002068 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002069 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002070 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002071
Sean Callanan79ed1a82010-01-19 20:22:31 +00002072 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002073
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002074 if (Str.empty())
2075 Error(Loc, ".abort detected. Assembly stopping.");
2076 else
2077 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002078 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002079
2080 return false;
2081}
Kevin Enderby71148242009-07-14 21:35:03 +00002082
Kevin Enderby1f049b22009-07-14 23:21:55 +00002083/// ParseDirectiveInclude
2084/// ::= .include "filename"
2085bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002086 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002087 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002088
Sean Callanan18b83232010-01-19 21:44:56 +00002089 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002090 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002091 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002092
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002093 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002094 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002095
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002096 // Strip the quotes.
2097 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002098
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002099 // Attempt to switch the lexer to the included file before consuming the end
2100 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002101 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002102 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002103 return true;
2104 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002105
2106 return false;
2107}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002108
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002109/// ParseDirectiveIf
2110/// ::= .if expression
2111bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002112 TheCondStack.push_back(TheCondState);
2113 TheCondState.TheCond = AsmCond::IfCond;
2114 if(TheCondState.Ignore) {
2115 EatToEndOfStatement();
2116 }
2117 else {
2118 int64_t ExprValue;
2119 if (ParseAbsoluteExpression(ExprValue))
2120 return true;
2121
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002122 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002123 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002124
Sean Callanan79ed1a82010-01-19 20:22:31 +00002125 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002126
2127 TheCondState.CondMet = ExprValue;
2128 TheCondState.Ignore = !TheCondState.CondMet;
2129 }
2130
2131 return false;
2132}
2133
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002134bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2135 StringRef Name;
2136 TheCondStack.push_back(TheCondState);
2137 TheCondState.TheCond = AsmCond::IfCond;
2138
2139 if (TheCondState.Ignore) {
2140 EatToEndOfStatement();
2141 } else {
2142 if (ParseIdentifier(Name))
2143 return TokError("expected identifier after '.ifdef'");
2144
2145 Lex();
2146
2147 MCSymbol *Sym = getContext().LookupSymbol(Name);
2148
2149 if (expect_defined)
2150 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2151 else
2152 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2153 TheCondState.Ignore = !TheCondState.CondMet;
2154 }
2155
2156 return false;
2157}
2158
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002159/// ParseDirectiveElseIf
2160/// ::= .elseif expression
2161bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2162 if (TheCondState.TheCond != AsmCond::IfCond &&
2163 TheCondState.TheCond != AsmCond::ElseIfCond)
2164 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2165 " an .elseif");
2166 TheCondState.TheCond = AsmCond::ElseIfCond;
2167
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002168 bool LastIgnoreState = false;
2169 if (!TheCondStack.empty())
2170 LastIgnoreState = TheCondStack.back().Ignore;
2171 if (LastIgnoreState || TheCondState.CondMet) {
2172 TheCondState.Ignore = true;
2173 EatToEndOfStatement();
2174 }
2175 else {
2176 int64_t ExprValue;
2177 if (ParseAbsoluteExpression(ExprValue))
2178 return true;
2179
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002180 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002181 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002182
Sean Callanan79ed1a82010-01-19 20:22:31 +00002183 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002184 TheCondState.CondMet = ExprValue;
2185 TheCondState.Ignore = !TheCondState.CondMet;
2186 }
2187
2188 return false;
2189}
2190
2191/// ParseDirectiveElse
2192/// ::= .else
2193bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002194 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002195 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002196
Sean Callanan79ed1a82010-01-19 20:22:31 +00002197 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002198
2199 if (TheCondState.TheCond != AsmCond::IfCond &&
2200 TheCondState.TheCond != AsmCond::ElseIfCond)
2201 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2202 ".elseif");
2203 TheCondState.TheCond = AsmCond::ElseCond;
2204 bool LastIgnoreState = false;
2205 if (!TheCondStack.empty())
2206 LastIgnoreState = TheCondStack.back().Ignore;
2207 if (LastIgnoreState || TheCondState.CondMet)
2208 TheCondState.Ignore = true;
2209 else
2210 TheCondState.Ignore = false;
2211
2212 return false;
2213}
2214
2215/// ParseDirectiveEndIf
2216/// ::= .endif
2217bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002218 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002219 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002220
Sean Callanan79ed1a82010-01-19 20:22:31 +00002221 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002222
2223 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2224 TheCondStack.empty())
2225 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2226 ".else");
2227 if (!TheCondStack.empty()) {
2228 TheCondState = TheCondStack.back();
2229 TheCondStack.pop_back();
2230 }
2231
2232 return false;
2233}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002234
2235/// ParseDirectiveFile
2236/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002237bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002238 // FIXME: I'm not sure what this is.
2239 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002240 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002241 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002242 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002243 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002244
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002245 if (FileNumber < 1)
2246 return TokError("file number less than one");
2247 }
2248
Daniel Dunbareceec052010-07-12 17:45:27 +00002249 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002250 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002251
Chris Lattnerd32e8032010-01-25 19:02:58 +00002252 StringRef Filename = getTok().getString();
2253 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002254 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002255
Daniel Dunbareceec052010-07-12 17:45:27 +00002256 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002257 return TokError("unexpected token in '.file' directive");
2258
Chris Lattnerd32e8032010-01-25 19:02:58 +00002259 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002260 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002261 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002262 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002263 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002264 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002265
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002266 return false;
2267}
2268
2269/// ParseDirectiveLine
2270/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002271bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002272 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2273 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002274 return TokError("unexpected token in '.line' directive");
2275
Sean Callanan18b83232010-01-19 21:44:56 +00002276 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002277 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002278 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002279
2280 // FIXME: Do something with the .line.
2281 }
2282
Daniel Dunbareceec052010-07-12 17:45:27 +00002283 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002284 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002285
2286 return false;
2287}
2288
2289
2290/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002291/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002292/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2293/// The first number is a file number, must have been previously assigned with
2294/// a .file directive, the second number is the line number and optionally the
2295/// third number is a column position (zero if not specified). The remaining
2296/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002297bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002298
Daniel Dunbareceec052010-07-12 17:45:27 +00002299 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002300 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002301 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002302 if (FileNumber < 1)
2303 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002304 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002305 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002306 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002307
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002308 int64_t LineNumber = 0;
2309 if (getLexer().is(AsmToken::Integer)) {
2310 LineNumber = getTok().getIntVal();
2311 if (LineNumber < 1)
2312 return TokError("line number less than one in '.loc' directive");
2313 Lex();
2314 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002315
2316 int64_t ColumnPos = 0;
2317 if (getLexer().is(AsmToken::Integer)) {
2318 ColumnPos = getTok().getIntVal();
2319 if (ColumnPos < 0)
2320 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002321 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002322 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002323
Kevin Enderbyc0957932010-09-30 16:52:03 +00002324 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002325 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002326 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002327 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2328 for (;;) {
2329 if (getLexer().is(AsmToken::EndOfStatement))
2330 break;
2331
2332 StringRef Name;
2333 SMLoc Loc = getTok().getLoc();
2334 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002335 return TokError("unexpected token in '.loc' directive");
2336
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002337 if (Name == "basic_block")
2338 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2339 else if (Name == "prologue_end")
2340 Flags |= DWARF2_FLAG_PROLOGUE_END;
2341 else if (Name == "epilogue_begin")
2342 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2343 else if (Name == "is_stmt") {
2344 SMLoc Loc = getTok().getLoc();
2345 const MCExpr *Value;
2346 if (getParser().ParseExpression(Value))
2347 return true;
2348 // The expression must be the constant 0 or 1.
2349 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2350 int Value = MCE->getValue();
2351 if (Value == 0)
2352 Flags &= ~DWARF2_FLAG_IS_STMT;
2353 else if (Value == 1)
2354 Flags |= DWARF2_FLAG_IS_STMT;
2355 else
2356 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002357 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002358 else {
2359 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2360 }
2361 }
2362 else if (Name == "isa") {
2363 SMLoc Loc = getTok().getLoc();
2364 const MCExpr *Value;
2365 if (getParser().ParseExpression(Value))
2366 return true;
2367 // The expression must be a constant greater or equal to 0.
2368 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2369 int Value = MCE->getValue();
2370 if (Value < 0)
2371 return Error(Loc, "isa number less than zero");
2372 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002373 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002374 else {
2375 return Error(Loc, "isa number not a constant value");
2376 }
2377 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002378 else if (Name == "discriminator") {
2379 if (getParser().ParseAbsoluteExpression(Discriminator))
2380 return true;
2381 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002382 else {
2383 return Error(Loc, "unknown sub-directive in '.loc' directive");
2384 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002385
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002386 if (getLexer().is(AsmToken::EndOfStatement))
2387 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002388 }
2389 }
2390
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002391 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002392 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002393
2394 return false;
2395}
2396
Daniel Dunbar138abae2010-10-16 04:56:42 +00002397/// ParseDirectiveStabs
2398/// ::= .stabs string, number, number, number
2399bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2400 SMLoc DirectiveLoc) {
2401 return TokError("unsupported directive '" + Directive + "'");
2402}
2403
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002404/// ParseDirectiveCFISections
2405/// ::= .cfi_sections section [, section]
2406bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2407 SMLoc DirectiveLoc) {
2408 StringRef Name;
2409 bool EH = false;
2410 bool Debug = false;
2411
2412 if (getParser().ParseIdentifier(Name))
2413 return TokError("Expected an identifier");
2414
2415 if (Name == ".eh_frame")
2416 EH = true;
2417 else if (Name == ".debug_frame")
2418 Debug = true;
2419
2420 if (getLexer().is(AsmToken::Comma)) {
2421 Lex();
2422
2423 if (getParser().ParseIdentifier(Name))
2424 return TokError("Expected an identifier");
2425
2426 if (Name == ".eh_frame")
2427 EH = true;
2428 else if (Name == ".debug_frame")
2429 Debug = true;
2430 }
2431
2432 getStreamer().EmitCFISections(EH, Debug);
2433
2434 return false;
2435}
2436
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002437/// ParseDirectiveCFIStartProc
2438/// ::= .cfi_startproc
2439bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2440 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002441 getStreamer().EmitCFIStartProc();
2442 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002443}
2444
2445/// ParseDirectiveCFIEndProc
2446/// ::= .cfi_endproc
2447bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002448 getStreamer().EmitCFIEndProc();
2449 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002450}
2451
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002452/// ParseRegisterOrRegisterNumber - parse register name or number.
2453bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2454 SMLoc DirectiveLoc) {
2455 unsigned RegNo;
2456
Jim Grosbach6f888a82011-06-02 17:14:04 +00002457 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002458 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2459 DirectiveLoc))
2460 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002461 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002462 } else
2463 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002464
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002465 return false;
2466}
2467
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002468/// ParseDirectiveCFIDefCfa
2469/// ::= .cfi_def_cfa register, offset
2470bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2471 SMLoc DirectiveLoc) {
2472 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002473 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002474 return true;
2475
2476 if (getLexer().isNot(AsmToken::Comma))
2477 return TokError("unexpected token in directive");
2478 Lex();
2479
2480 int64_t Offset = 0;
2481 if (getParser().ParseAbsoluteExpression(Offset))
2482 return true;
2483
Rafael Espindola066c2f42011-04-12 23:59:07 +00002484 getStreamer().EmitCFIDefCfa(Register, Offset);
2485 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002486}
2487
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002488/// ParseDirectiveCFIDefCfaOffset
2489/// ::= .cfi_def_cfa_offset offset
2490bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2491 SMLoc DirectiveLoc) {
2492 int64_t Offset = 0;
2493 if (getParser().ParseAbsoluteExpression(Offset))
2494 return true;
2495
Rafael Espindola066c2f42011-04-12 23:59:07 +00002496 getStreamer().EmitCFIDefCfaOffset(Offset);
2497 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002498}
2499
2500/// ParseDirectiveCFIAdjustCfaOffset
2501/// ::= .cfi_adjust_cfa_offset adjustment
2502bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2503 SMLoc DirectiveLoc) {
2504 int64_t Adjustment = 0;
2505 if (getParser().ParseAbsoluteExpression(Adjustment))
2506 return true;
2507
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002508 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2509 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002510}
2511
2512/// ParseDirectiveCFIDefCfaRegister
2513/// ::= .cfi_def_cfa_register register
2514bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2515 SMLoc DirectiveLoc) {
2516 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002517 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002518 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002519
Rafael Espindola066c2f42011-04-12 23:59:07 +00002520 getStreamer().EmitCFIDefCfaRegister(Register);
2521 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002522}
2523
2524/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002525/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002526bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2527 int64_t Register = 0;
2528 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002529
2530 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002531 return true;
2532
2533 if (getLexer().isNot(AsmToken::Comma))
2534 return TokError("unexpected token in directive");
2535 Lex();
2536
2537 if (getParser().ParseAbsoluteExpression(Offset))
2538 return true;
2539
Rafael Espindola066c2f42011-04-12 23:59:07 +00002540 getStreamer().EmitCFIOffset(Register, Offset);
2541 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002542}
2543
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002544/// ParseDirectiveCFIRelOffset
2545/// ::= .cfi_rel_offset register, offset
2546bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2547 SMLoc DirectiveLoc) {
2548 int64_t Register = 0;
2549
2550 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2551 return true;
2552
2553 if (getLexer().isNot(AsmToken::Comma))
2554 return TokError("unexpected token in directive");
2555 Lex();
2556
2557 int64_t Offset = 0;
2558 if (getParser().ParseAbsoluteExpression(Offset))
2559 return true;
2560
Rafael Espindola25f492e2011-04-12 16:12:03 +00002561 getStreamer().EmitCFIRelOffset(Register, Offset);
2562 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002563}
2564
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002565static bool isValidEncoding(int64_t Encoding) {
2566 if (Encoding & ~0xff)
2567 return false;
2568
2569 if (Encoding == dwarf::DW_EH_PE_omit)
2570 return true;
2571
2572 const unsigned Format = Encoding & 0xf;
2573 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2574 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2575 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2576 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2577 return false;
2578
Rafael Espindolacaf11582010-12-29 04:31:26 +00002579 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002580 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002581 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002582 return false;
2583
2584 return true;
2585}
2586
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002587/// ParseDirectiveCFIPersonalityOrLsda
2588/// ::= .cfi_personality encoding, [symbol_name]
2589/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002590bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002591 SMLoc DirectiveLoc) {
2592 int64_t Encoding = 0;
2593 if (getParser().ParseAbsoluteExpression(Encoding))
2594 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002595 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002596 return false;
2597
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002598 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002599 return TokError("unsupported encoding.");
2600
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002601 if (getLexer().isNot(AsmToken::Comma))
2602 return TokError("unexpected token in directive");
2603 Lex();
2604
2605 StringRef Name;
2606 if (getParser().ParseIdentifier(Name))
2607 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002608
2609 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2610
2611 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002612 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002613 else {
2614 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002615 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002616 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002617 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002618}
2619
Rafael Espindolafe024d02010-12-28 18:36:23 +00002620/// ParseDirectiveCFIRememberState
2621/// ::= .cfi_remember_state
2622bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2623 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002624 getStreamer().EmitCFIRememberState();
2625 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002626}
2627
2628/// ParseDirectiveCFIRestoreState
2629/// ::= .cfi_remember_state
2630bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2631 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002632 getStreamer().EmitCFIRestoreState();
2633 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002634}
2635
Rafael Espindolac5754392011-04-12 15:31:05 +00002636/// ParseDirectiveCFISameValue
2637/// ::= .cfi_same_value register
2638bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2639 SMLoc DirectiveLoc) {
2640 int64_t Register = 0;
2641
2642 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2643 return true;
2644
2645 getStreamer().EmitCFISameValue(Register);
2646
2647 return false;
2648}
2649
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002650/// ParseDirectiveMacrosOnOff
2651/// ::= .macros_on
2652/// ::= .macros_off
2653bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2654 SMLoc DirectiveLoc) {
2655 if (getLexer().isNot(AsmToken::EndOfStatement))
2656 return Error(getLexer().getLoc(),
2657 "unexpected token in '" + Directive + "' directive");
2658
2659 getParser().MacrosEnabled = Directive == ".macros_on";
2660
2661 return false;
2662}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002663
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002664/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002665/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002666bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2667 SMLoc DirectiveLoc) {
2668 StringRef Name;
2669 if (getParser().ParseIdentifier(Name))
2670 return TokError("expected identifier in directive");
2671
Rafael Espindola65366442011-06-05 02:43:45 +00002672 std::vector<StringRef> Parameters;
2673 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2674 for(;;) {
2675 StringRef Parameter;
2676 if (getParser().ParseIdentifier(Parameter))
2677 return TokError("expected identifier in directive");
2678 Parameters.push_back(Parameter);
2679
2680 if (getLexer().isNot(AsmToken::Comma))
2681 break;
2682 Lex();
2683 }
2684 }
2685
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002686 if (getLexer().isNot(AsmToken::EndOfStatement))
2687 return TokError("unexpected token in '.macro' directive");
2688
2689 // Eat the end of statement.
2690 Lex();
2691
2692 AsmToken EndToken, StartToken = getTok();
2693
2694 // Lex the macro definition.
2695 for (;;) {
2696 // Check whether we have reached the end of the file.
2697 if (getLexer().is(AsmToken::Eof))
2698 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2699
2700 // Otherwise, check whether we have reach the .endmacro.
2701 if (getLexer().is(AsmToken::Identifier) &&
2702 (getTok().getIdentifier() == ".endm" ||
2703 getTok().getIdentifier() == ".endmacro")) {
2704 EndToken = getTok();
2705 Lex();
2706 if (getLexer().isNot(AsmToken::EndOfStatement))
2707 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2708 "' directive");
2709 break;
2710 }
2711
2712 // Otherwise, scan til the end of the statement.
2713 getParser().EatToEndOfStatement();
2714 }
2715
2716 if (getParser().MacroMap.lookup(Name)) {
2717 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2718 }
2719
2720 const char *BodyStart = StartToken.getLoc().getPointer();
2721 const char *BodyEnd = EndToken.getLoc().getPointer();
2722 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002723 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002724 return false;
2725}
2726
2727/// ParseDirectiveEndMacro
2728/// ::= .endm
2729/// ::= .endmacro
2730bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2731 SMLoc DirectiveLoc) {
2732 if (getLexer().isNot(AsmToken::EndOfStatement))
2733 return TokError("unexpected token in '" + Directive + "' directive");
2734
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002735 // If we are inside a macro instantiation, terminate the current
2736 // instantiation.
2737 if (!getParser().ActiveMacros.empty()) {
2738 getParser().HandleMacroExit();
2739 return false;
2740 }
2741
2742 // Otherwise, this .endmacro is a stray entry in the file; well formed
2743 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002744 return TokError("unexpected '" + Directive + "' in file, "
2745 "no current macro definition");
2746}
2747
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002748bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002749 getParser().CheckForValidSection();
2750
2751 const MCExpr *Value;
2752
2753 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002754 return true;
2755
2756 if (getLexer().isNot(AsmToken::EndOfStatement))
2757 return TokError("unexpected token in directive");
2758
2759 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002760 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002761 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002762 getStreamer().EmitULEB128Value(Value);
2763
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002764 return false;
2765}
2766
2767
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002768/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002769MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002770 MCContext &C, MCStreamer &Out,
2771 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002772 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002773}