blob: 311c3347a54093e0f14b3dc343e8e2aed6bcd7ce [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
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000118 /// The values from the last parsed cpp hash file line comment if any.
119 StringRef CppHashFilename;
120 int64_t CppHashLineNumber;
121 SMLoc CppHashLoc;
122
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000123public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000124 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000125 const MCAsmInfo &MAI);
126 ~AsmParser();
127
128 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
129
130 void AddDirectiveHandler(MCAsmParserExtension *Object,
131 StringRef Directive,
132 DirectiveHandler Handler) {
133 DirectiveMap[Directive] = std::make_pair(Object, Handler);
134 }
135
136public:
137 /// @name MCAsmParser Interface
138 /// {
139
140 virtual SourceMgr &getSourceManager() { return SrcMgr; }
141 virtual MCAsmLexer &getLexer() { return Lexer; }
142 virtual MCContext &getContext() { return Ctx; }
143 virtual MCStreamer &getStreamer() { return Out; }
144
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000145 virtual bool Warning(SMLoc L, const Twine &Msg);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000146 virtual bool Error(SMLoc L, const Twine &Msg);
147
148 const AsmToken &Lex();
149
150 bool ParseExpression(const MCExpr *&Res);
151 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
152 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
153 virtual bool ParseAbsoluteExpression(int64_t &Res);
154
155 /// }
156
157private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000158 void CheckForValidSection();
159
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000160 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000161 void EatToEndOfLine();
162 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000164 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000165 bool expandMacro(SmallString<256> &Buf, StringRef Body,
166 const std::vector<StringRef> &Parameters,
167 const std::vector<std::vector<AsmToken> > &A,
168 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000169 void HandleMacroExit();
170
171 void PrintMacroInstantiations();
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000172 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type,
173 bool ShowLine = true) const {
174 SrcMgr.PrintMessage(Loc, Msg, Type, ShowLine);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000175 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000176 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000177
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000178 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
179 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000180
181 /// \brief Reset the current lexer position to that given by \arg Loc. The
182 /// current token is not set; clients should ensure Lex() is called
183 /// subsequently.
184 void JumpToLoc(SMLoc Loc);
185
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000186 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000187
188 /// \brief Parse up to the end of statement and a return the contents from the
189 /// current token until the end of the statement; the current token on exit
190 /// will be either the EndOfStatement or EOF.
191 StringRef ParseStringToEndOfStatement();
192
Nico Weber4c4c7322011-01-28 03:04:41 +0000193 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000194
195 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
196 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
197 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000198 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000199
200 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
201 /// and set \arg Res to the identifier contents.
202 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000203
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000204 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000205
206 // ".ascii", ".asciiz", ".string"
207 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000208 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000209 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000210 bool ParseDirectiveFill(); // ".fill"
211 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000212 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000213 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000214 bool ParseDirectiveOrg(); // ".org"
215 // ".align{,32}", ".p2align{,w,l}"
216 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
217
218 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
219 /// accepts a single symbol (which should be a label or an external).
220 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000221
222 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
223
224 bool ParseDirectiveAbort(); // ".abort"
225 bool ParseDirectiveInclude(); // ".include"
226
227 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000228 // ".ifdef" or ".ifndef", depending on expect_defined
229 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000230 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
231 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
232 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
233
234 /// ParseEscapedString - Parse the current token as a string which may include
235 /// escaped characters and return the string contents.
236 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000237
238 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
239 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000240};
241
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000242/// \brief Generic implementations of directive handling, etc. which is shared
243/// (or the default, at least) for all assembler parser.
244class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000245 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
246 void AddDirectiveHandler(StringRef Directive) {
247 getParser().AddDirectiveHandler(this, Directive,
248 HandleDirective<GenericAsmParser, Handler>);
249 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000250public:
251 GenericAsmParser() {}
252
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000253 AsmParser &getParser() {
254 return (AsmParser&) this->MCAsmParserExtension::getParser();
255 }
256
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000257 virtual void Initialize(MCAsmParser &Parser) {
258 // Call the base implementation.
259 this->MCAsmParserExtension::Initialize(Parser);
260
261 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000262 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
263 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
264 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000266
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000267 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
269 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000270 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
271 ".cfi_startproc");
272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
273 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
275 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
277 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000278 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
279 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000280 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
281 ".cfi_def_cfa_register");
282 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
283 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000284 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
285 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000286 AddDirectiveHandler<
287 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
288 AddDirectiveHandler<
289 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000290 AddDirectiveHandler<
291 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
292 AddDirectiveHandler<
293 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000294 AddDirectiveHandler<
295 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000296
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000297 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000298 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
299 ".macros_on");
300 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
301 ".macros_off");
302 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
303 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000305
306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
307 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000308 }
309
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000310 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
311
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000312 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
313 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
314 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000315 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000316 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000317 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
318 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000319 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000320 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000321 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000322 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
323 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000324 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000325 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000326 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
327 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000328 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000329
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000330 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000331 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
332 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000333
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000334 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000335};
336
337}
338
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000339namespace llvm {
340
341extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000342extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000343extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000344
345}
346
Chris Lattneraaec2052010-01-19 19:46:13 +0000347enum { DEFAULT_ADDRSPACE = 0 };
348
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000349AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000350 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000351 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000352 GenericParser(new GenericAsmParser), PlatformParser(0),
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000353 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0) {
354 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000355 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000356
357 // Initialize the generic parser.
358 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000359
360 // Initialize the platform / file format parser.
361 //
362 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
363 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000364 if (_MAI.hasMicrosoftFastStdCallMangling()) {
365 PlatformParser = createCOFFAsmParser();
366 PlatformParser->Initialize(*this);
367 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000368 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000369 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000370 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000371 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000372 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000373 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000374}
375
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000376AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000377 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
378
379 // Destroy any macros.
380 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
381 ie = MacroMap.end(); it != ie; ++it)
382 delete it->getValue();
383
Daniel Dunbare4749702010-07-12 18:12:02 +0000384 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000385 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000386}
387
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000388void AsmParser::PrintMacroInstantiations() {
389 // Print the active macro instantiation stack.
390 for (std::vector<MacroInstantiation*>::const_reverse_iterator
391 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
392 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
393 "note");
394}
395
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000396bool AsmParser::Warning(SMLoc L, const Twine &Msg) {
397 if (FatalAssemblerWarnings)
398 return Error(L, Msg);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000399 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000400 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000401 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000402}
403
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000404bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000405 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000406 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000407 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000408 return true;
409}
410
Sean Callananfd0b0282010-01-21 00:19:58 +0000411bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000412 std::string IncludedFile;
413 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000414 if (NewBuf == -1)
415 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000416
Sean Callananfd0b0282010-01-21 00:19:58 +0000417 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000418
Sean Callananfd0b0282010-01-21 00:19:58 +0000419 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000420
Sean Callananfd0b0282010-01-21 00:19:58 +0000421 return false;
422}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000423
424void AsmParser::JumpToLoc(SMLoc Loc) {
425 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
426 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
427}
428
Sean Callananfd0b0282010-01-21 00:19:58 +0000429const AsmToken &AsmParser::Lex() {
430 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000431
Sean Callananfd0b0282010-01-21 00:19:58 +0000432 if (tok->is(AsmToken::Eof)) {
433 // If this is the end of an included file, pop the parent file off the
434 // include stack.
435 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
436 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000437 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000438 tok = &Lexer.Lex();
439 }
440 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000441
Sean Callananfd0b0282010-01-21 00:19:58 +0000442 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000443 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000444
Sean Callananfd0b0282010-01-21 00:19:58 +0000445 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000446}
447
Chris Lattner79180e22010-04-05 23:15:42 +0000448bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000449 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000450 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000451 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000452
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000453 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000454 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000455
456 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000457 AsmCond StartingCondState = TheCondState;
458
Chris Lattnerb717fb02009-07-02 21:53:43 +0000459 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000460 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000461 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000462
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000463 // We had an error, validate that one was emitted and recover by skipping to
464 // the next line.
465 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000466 EatToEndOfStatement();
467 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000468
469 if (TheCondState.TheCond != StartingCondState.TheCond ||
470 TheCondState.Ignore != StartingCondState.Ignore)
471 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000472
473 // Check to see there are no empty DwarfFile slots.
474 const std::vector<MCDwarfFile *> &MCDwarfFiles =
475 getContext().getMCDwarfFiles();
476 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000477 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000478 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000479 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000480
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000481 // Check to see that all assembler local symbols were actually defined.
482 // Targets that don't do subsections via symbols may not want this, though,
483 // so conservatively exclude them. Only do this if we're finalizing, though,
484 // as otherwise we won't necessarilly have seen everything yet.
485 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
486 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
487 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
488 e = Symbols.end();
489 i != e; ++i) {
490 MCSymbol *Sym = i->getValue();
491 // Variable symbols may not be marked as defined, so check those
492 // explicitly. If we know it's a variable, we have a definition for
493 // the purposes of this check.
494 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
495 // FIXME: We would really like to refer back to where the symbol was
496 // first referenced for a source location. We need to add something
497 // to track that. Currently, we just point to the end of the file.
498 PrintMessage(getLexer().getLoc(), "assembler local symbol '" +
499 Sym->getName() + "' not defined", "error", false);
500 }
501 }
502
503
Chris Lattner79180e22010-04-05 23:15:42 +0000504 // Finalize the output stream if there are no errors and if the client wants
505 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000506 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000507 Out.Finish();
508
Chris Lattnerb717fb02009-07-02 21:53:43 +0000509 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000510}
511
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000512void AsmParser::CheckForValidSection() {
513 if (!getStreamer().getCurrentSection()) {
514 TokError("expected section directive before assembly directive");
515 Out.SwitchSection(Ctx.getMachOSection(
516 "__TEXT", "__text",
517 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
518 0, SectionKind::getText()));
519 }
520}
521
Chris Lattner2cf5f142009-06-22 01:29:09 +0000522/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
523void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000524 while (Lexer.isNot(AsmToken::EndOfStatement) &&
525 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000526 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000527
Chris Lattner2cf5f142009-06-22 01:29:09 +0000528 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000529 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000530 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000531}
532
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000533StringRef AsmParser::ParseStringToEndOfStatement() {
534 const char *Start = getTok().getLoc().getPointer();
535
536 while (Lexer.isNot(AsmToken::EndOfStatement) &&
537 Lexer.isNot(AsmToken::Eof))
538 Lex();
539
540 const char *End = getTok().getLoc().getPointer();
541 return StringRef(Start, End - Start);
542}
Chris Lattnerc4193832009-06-22 05:51:26 +0000543
Chris Lattner74ec1a32009-06-22 06:32:03 +0000544/// ParseParenExpr - Parse a paren expression and return it.
545/// NOTE: This assumes the leading '(' has already been consumed.
546///
547/// parenexpr ::= expr)
548///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000549bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000550 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000551 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000552 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000553 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000554 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000555 return false;
556}
Chris Lattnerc4193832009-06-22 05:51:26 +0000557
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000558/// ParseBracketExpr - Parse a bracket expression and return it.
559/// NOTE: This assumes the leading '[' has already been consumed.
560///
561/// bracketexpr ::= expr]
562///
563bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
564 if (ParseExpression(Res)) return true;
565 if (Lexer.isNot(AsmToken::RBrac))
566 return TokError("expected ']' in brackets expression");
567 EndLoc = Lexer.getLoc();
568 Lex();
569 return false;
570}
571
Chris Lattner74ec1a32009-06-22 06:32:03 +0000572/// ParsePrimaryExpr - Parse a primary expression and return it.
573/// primaryexpr ::= (parenexpr
574/// primaryexpr ::= symbol
575/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000576/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000577/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000578bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000579 switch (Lexer.getKind()) {
580 default:
581 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000582 // If we have an error assume that we've already handled it.
583 case AsmToken::Error:
584 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000585 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000586 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000587 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000588 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000589 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000590 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000591 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000592 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000593 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000594 EndLoc = Lexer.getLoc();
595
596 StringRef Identifier;
597 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000598 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000599
Daniel Dunbarfffff912009-10-16 01:34:54 +0000600 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000601 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000602 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000603
604 // Lookup the symbol variant if used.
605 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000606 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000607 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000608 if (Variant == MCSymbolRefExpr::VK_Invalid) {
609 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000610 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000611 }
612 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000613
Daniel Dunbarfffff912009-10-16 01:34:54 +0000614 // If this is an absolute variable reference, substitute it now to preserve
615 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000616 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000617 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000618 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000619
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000620 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000621 return false;
622 }
623
624 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000625 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000626 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000627 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000628 case AsmToken::Integer: {
629 SMLoc Loc = getTok().getLoc();
630 int64_t IntVal = getTok().getIntVal();
631 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000632 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000633 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000634 // Look for 'b' or 'f' following an Integer as a directional label
635 if (Lexer.getKind() == AsmToken::Identifier) {
636 StringRef IDVal = getTok().getString();
637 if (IDVal == "f" || IDVal == "b"){
638 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
639 IDVal == "f" ? 1 : 0);
640 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
641 getContext());
642 if(IDVal == "b" && Sym->isUndefined())
643 return Error(Loc, "invalid reference to undefined symbol");
644 EndLoc = Lexer.getLoc();
645 Lex(); // Eat identifier.
646 }
647 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000648 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000649 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000650 case AsmToken::Real: {
651 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000652 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000653 Res = MCConstantExpr::Create(IntVal, getContext());
654 Lex(); // Eat token.
655 return false;
656 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000657 case AsmToken::Dot: {
658 // This is a '.' reference, which references the current PC. Emit a
659 // temporary label to the streamer and refer to it.
660 MCSymbol *Sym = Ctx.CreateTempSymbol();
661 Out.EmitLabel(Sym);
662 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
663 EndLoc = Lexer.getLoc();
664 Lex(); // Eat identifier.
665 return false;
666 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000667 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000668 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000669 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000670 case AsmToken::LBrac:
671 if (!PlatformParser->HasBracketExpressions())
672 return TokError("brackets expression not supported on this target");
673 Lex(); // Eat the '['.
674 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000675 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000676 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000677 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000678 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000679 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000680 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000681 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000682 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000683 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000684 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000685 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000686 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000687 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000688 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000689 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000690 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000691 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000692 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000693 }
694}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000695
Chris Lattnerb4307b32010-01-15 19:28:38 +0000696bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000697 SMLoc EndLoc;
698 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000699}
700
Daniel Dunbarcceba832010-09-17 02:47:07 +0000701const MCExpr *
702AsmParser::ApplyModifierToExpr(const MCExpr *E,
703 MCSymbolRefExpr::VariantKind Variant) {
704 // Recurse over the given expression, rebuilding it to apply the given variant
705 // if there is exactly one symbol.
706 switch (E->getKind()) {
707 case MCExpr::Target:
708 case MCExpr::Constant:
709 return 0;
710
711 case MCExpr::SymbolRef: {
712 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
713
714 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
715 TokError("invalid variant on expression '" +
716 getTok().getIdentifier() + "' (already modified)");
717 return E;
718 }
719
720 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
721 }
722
723 case MCExpr::Unary: {
724 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
725 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
726 if (!Sub)
727 return 0;
728 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
729 }
730
731 case MCExpr::Binary: {
732 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
733 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
734 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
735
736 if (!LHS && !RHS)
737 return 0;
738
739 if (!LHS) LHS = BE->getLHS();
740 if (!RHS) RHS = BE->getRHS();
741
742 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
743 }
744 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000745
746 assert(0 && "Invalid expression kind!");
747 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000748}
749
Chris Lattner74ec1a32009-06-22 06:32:03 +0000750/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000751///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000752/// expr ::= expr &&,|| expr -> lowest.
753/// expr ::= expr |,^,&,! expr
754/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
755/// expr ::= expr <<,>> expr
756/// expr ::= expr +,- expr
757/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000758/// expr ::= primaryexpr
759///
Chris Lattner54482b42010-01-15 19:39:23 +0000760bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000761 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000762 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000763 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
764 return true;
765
Daniel Dunbarcceba832010-09-17 02:47:07 +0000766 // As a special case, we support 'a op b @ modifier' by rewriting the
767 // expression to include the modifier. This is inefficient, but in general we
768 // expect users to use 'a@modifier op b'.
769 if (Lexer.getKind() == AsmToken::At) {
770 Lex();
771
772 if (Lexer.isNot(AsmToken::Identifier))
773 return TokError("unexpected symbol modifier following '@'");
774
775 MCSymbolRefExpr::VariantKind Variant =
776 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
777 if (Variant == MCSymbolRefExpr::VK_Invalid)
778 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
779
780 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
781 if (!ModifiedRes) {
782 return TokError("invalid modifier '" + getTok().getIdentifier() +
783 "' (no symbols present)");
784 return true;
785 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000786
Daniel Dunbarcceba832010-09-17 02:47:07 +0000787 Res = ModifiedRes;
788 Lex();
789 }
790
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000791 // Try to constant fold it up front, if possible.
792 int64_t Value;
793 if (Res->EvaluateAsAbsolute(Value))
794 Res = MCConstantExpr::Create(Value, getContext());
795
796 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000797}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000798
Chris Lattnerb4307b32010-01-15 19:28:38 +0000799bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000800 Res = 0;
801 return ParseParenExpr(Res, EndLoc) ||
802 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000803}
804
Daniel Dunbar475839e2009-06-29 20:37:27 +0000805bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000806 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000807
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000808 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000809 if (ParseExpression(Expr))
810 return true;
811
Daniel Dunbare00b0112009-10-16 01:57:52 +0000812 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000813 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000814
815 return false;
816}
817
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000818static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000819 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000820 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000821 default:
822 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000823
Jim Grosbachfbe16812011-08-20 16:24:13 +0000824 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000825 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000826 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000827 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000828 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000829 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000830 return 1;
831
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000832
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000833 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000834 //
835 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000836 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000837 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000838 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000839 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000840 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000841 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000842 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000843 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000844 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000845
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000846 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000847 case AsmToken::EqualEqual:
848 Kind = MCBinaryExpr::EQ;
849 return 3;
850 case AsmToken::ExclaimEqual:
851 case AsmToken::LessGreater:
852 Kind = MCBinaryExpr::NE;
853 return 3;
854 case AsmToken::Less:
855 Kind = MCBinaryExpr::LT;
856 return 3;
857 case AsmToken::LessEqual:
858 Kind = MCBinaryExpr::LTE;
859 return 3;
860 case AsmToken::Greater:
861 Kind = MCBinaryExpr::GT;
862 return 3;
863 case AsmToken::GreaterEqual:
864 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000865 return 3;
866
Jim Grosbachfbe16812011-08-20 16:24:13 +0000867 // Intermediate Precedence: <<, >>
868 case AsmToken::LessLess:
869 Kind = MCBinaryExpr::Shl;
870 return 4;
871 case AsmToken::GreaterGreater:
872 Kind = MCBinaryExpr::Shr;
873 return 4;
874
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000875 // High Intermediate Precedence: +, -
876 case AsmToken::Plus:
877 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000878 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000879 case AsmToken::Minus:
880 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000881 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000882
Jim Grosbachfbe16812011-08-20 16:24:13 +0000883 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000884 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000885 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000886 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000887 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000888 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000889 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000890 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000891 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000892 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000893 }
894}
895
896
897/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
898/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000899bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
900 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000901 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000902 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000903 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000904
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000905 // If the next token is lower precedence than we are allowed to eat, return
906 // successfully with what we ate already.
907 if (TokPrec < Precedence)
908 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000909
Sean Callanan79ed1a82010-01-19 20:22:31 +0000910 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000911
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000912 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000913 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000914 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000915
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000916 // If BinOp binds less tightly with RHS than the operator after RHS, let
917 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000918 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000919 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000920 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000921 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000922 }
923
Daniel Dunbar475839e2009-06-29 20:37:27 +0000924 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000925 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000926 }
927}
928
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000929
930
931
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000932/// ParseStatement:
933/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000934/// ::= Label* Directive ...Operands... EndOfStatement
935/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000936bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000937 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000938 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000939 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000940 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000941 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000942
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000943 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000944 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000945 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000946 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000947 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000948 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000949 if (Lexer.is(AsmToken::Hash))
950 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000951
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000952 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000953 if (Lexer.is(AsmToken::Integer)) {
954 LocalLabelVal = getTok().getIntVal();
955 if (LocalLabelVal < 0) {
956 if (!TheCondState.Ignore)
957 return TokError("unexpected token at start of statement");
958 IDVal = "";
959 }
960 else {
961 IDVal = getTok().getString();
962 Lex(); // Consume the integer token to be used as an identifier token.
963 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000964 if (!TheCondState.Ignore)
965 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000966 }
967 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000968
969 } else if (Lexer.is(AsmToken::Dot)) {
970 // Treat '.' as a valid identifier in this context.
971 Lex();
972 IDVal = ".";
973
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000974 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000975 if (!TheCondState.Ignore)
976 return TokError("unexpected token at start of statement");
977 IDVal = "";
978 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000979
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000980
Chris Lattner7834fac2010-04-17 18:14:27 +0000981 // Handle conditional assembly here before checking for skipping. We
982 // have to do this so that .endif isn't skipped in a ".if 0" block for
983 // example.
984 if (IDVal == ".if")
985 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000986 if (IDVal == ".ifdef")
987 return ParseDirectiveIfdef(IDLoc, true);
988 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
989 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000990 if (IDVal == ".elseif")
991 return ParseDirectiveElseIf(IDLoc);
992 if (IDVal == ".else")
993 return ParseDirectiveElse(IDLoc);
994 if (IDVal == ".endif")
995 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000996
Chris Lattner7834fac2010-04-17 18:14:27 +0000997 // If we are in a ".if 0" block, ignore this statement.
998 if (TheCondState.Ignore) {
999 EatToEndOfStatement();
1000 return false;
1001 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001002
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001003 // FIXME: Recurse on local labels?
1004
1005 // See what kind of statement we have.
1006 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001007 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001008 CheckForValidSection();
1009
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001010 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001011 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001012
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001013 // Diagnose attempt to use '.' as a label.
1014 if (IDVal == ".")
1015 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1016
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001017 // Diagnose attempt to use a variable as a label.
1018 //
1019 // FIXME: Diagnostics. Note the location of the definition as a label.
1020 // FIXME: This doesn't diagnose assignment to a symbol which has been
1021 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001022 MCSymbol *Sym;
1023 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001024 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001025 else
1026 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001027 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001028 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001029
Daniel Dunbar959fd882009-08-26 22:13:22 +00001030 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001031 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001032
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001033 // Consume any end of statement token, if present, to avoid spurious
1034 // AddBlankLine calls().
1035 if (Lexer.is(AsmToken::EndOfStatement)) {
1036 Lex();
1037 if (Lexer.is(AsmToken::Eof))
1038 return false;
1039 }
1040
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001041 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001042 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001043
Daniel Dunbar3f872332009-07-28 16:08:33 +00001044 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001045 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001046 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001047
Nico Weber4c4c7322011-01-28 03:04:41 +00001048 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001049
1050 default: // Normal instruction or directive.
1051 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001052 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001053
1054 // If macros are enabled, check to see if this is a macro instantiation.
1055 if (MacrosEnabled)
1056 if (const Macro *M = MacroMap.lookup(IDVal))
1057 return HandleMacroEntry(IDVal, IDLoc, M);
1058
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001059 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001060 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001061 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001062 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001063 return ParseDirectiveSet(IDVal, true);
1064 if (IDVal == ".equiv")
1065 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001066
Daniel Dunbara0d14262009-06-24 23:30:00 +00001067 // Data directives
1068
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001069 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001070 return ParseDirectiveAscii(IDVal, false);
1071 if (IDVal == ".asciz" || IDVal == ".string")
1072 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001073
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001074 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001075 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001076 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001078 if (IDVal == ".value")
1079 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001080 if (IDVal == ".2byte")
1081 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001082 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001083 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001084 if (IDVal == ".int")
1085 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001086 if (IDVal == ".4byte")
1087 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001088 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001089 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001090 if (IDVal == ".8byte")
1091 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001092 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001093 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1094 if (IDVal == ".double")
1095 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001096
Eli Friedman5d68ec22010-07-19 04:17:25 +00001097 if (IDVal == ".align") {
1098 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1099 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1100 }
1101 if (IDVal == ".align32") {
1102 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1103 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1104 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001105 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001106 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001107 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001108 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001109 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001110 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001111 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001112 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001113 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001114 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001115 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001116 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1117
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001118 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001119 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001120
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001121 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001122 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001123 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001124 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001125 if (IDVal == ".zero")
1126 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001127
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001128 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001129
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001130 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001131 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001132 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001133 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001134 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001135 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001136 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001137 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001138 if (IDVal == ".symbol_resolver")
1139 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001140 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001141 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001142 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001143 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001144 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001145 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001146 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001147 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001148 if (IDVal == ".weak_def_can_be_hidden")
1149 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001150
Hans Wennborg5cc64912011-06-18 13:51:54 +00001151 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001152 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001153 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001154 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001155
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001156 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001157 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001158 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001159 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001160
Evan Chengbd27f5a2011-07-27 00:38:12 +00001161 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001162 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001163
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001164 // Look up the handler in the handler table.
1165 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1166 DirectiveMap.lookup(IDVal);
1167 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001168 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001169
Kevin Enderby9c656452009-09-10 20:51:44 +00001170 // Target hook for parsing target specific directives.
1171 if (!getTargetParser().ParseDirective(ID))
1172 return false;
1173
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001174 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001175 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001176 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001177 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001178
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001179 CheckForValidSection();
1180
Chris Lattnera7f13542010-05-19 23:34:33 +00001181 // Canonicalize the opcode to lower case.
1182 SmallString<128> Opcode;
1183 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1184 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001185
Chris Lattner98986712010-01-14 22:21:20 +00001186 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001187 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001188 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001189
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001190 // Dump the parsed representation, if requested.
1191 if (getShowParsedOperands()) {
1192 SmallString<256> Str;
1193 raw_svector_ostream OS(Str);
1194 OS << "parsed instruction: [";
1195 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1196 if (i != 0)
1197 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001198 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001199 }
1200 OS << "]";
1201
1202 PrintMessage(IDLoc, OS.str(), "note");
1203 }
1204
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001205 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001206 if (!HadError)
1207 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1208 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001209
Chris Lattner98986712010-01-14 22:21:20 +00001210 // Free any parsed operands.
1211 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1212 delete ParsedOperands[i];
1213
Chris Lattnercbf8a982010-09-11 16:18:25 +00001214 // Don't skip the rest of the line, the instruction parser is responsible for
1215 // that.
1216 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001217}
Chris Lattner9a023f72009-06-24 04:43:34 +00001218
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001219/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1220/// since they may not be able to be tokenized to get to the end of line token.
1221void AsmParser::EatToEndOfLine() {
1222 Lexer.LexUntilEndOfLine();
1223 // Eat EOL.
1224 Lex();
1225}
1226
1227/// ParseCppHashLineFilenameComment as this:
1228/// ::= # number "filename"
1229/// or just as a full line comment if it doesn't have a number and a string.
1230bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1231 Lex(); // Eat the hash token.
1232
1233 if (getLexer().isNot(AsmToken::Integer)) {
1234 // Consume the line since in cases it is not a well-formed line directive,
1235 // as if were simply a full line comment.
1236 EatToEndOfLine();
1237 return false;
1238 }
1239
1240 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001241 Lex();
1242
1243 if (getLexer().isNot(AsmToken::String)) {
1244 EatToEndOfLine();
1245 return false;
1246 }
1247
1248 StringRef Filename = getTok().getString();
1249 // Get rid of the enclosing quotes.
1250 Filename = Filename.substr(1, Filename.size()-2);
1251
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001252 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1253 CppHashLoc = L;
1254 CppHashFilename = Filename;
1255 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001256
1257 // Ignore any trailing characters, they're just comment.
1258 EatToEndOfLine();
1259 return false;
1260}
1261
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001262/// DiagHandler - will use the the last parsed cpp hash line filename comment
1263/// for the Filename and LineNo if any in the diagnostic.
1264void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1265 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1266 raw_ostream &OS = errs();
1267
1268 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1269 const SMLoc &DiagLoc = Diag.getLoc();
1270 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1271 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1272
1273 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1274 // before printing the message.
1275 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1276 if (DiagCurBuffer > 0) {
1277 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1278 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1279 }
1280
1281 // If we have not parsed a cpp hash line filename comment or the source
1282 // manager changed or buffer changed (like in a nested include) then just
1283 // print the normal diagnostic using its Filename and LineNo.
1284 if (!Parser->CppHashLineNumber ||
1285 &DiagSrcMgr != &Parser->SrcMgr ||
1286 DiagBuf != CppHashBuf) {
1287 Diag.Print(0, OS);
1288 return;
1289 }
1290
1291 // Use the CppHashFilename and calculate a line number based on the
1292 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1293 // the diagnostic.
1294 const std::string Filename = Parser->CppHashFilename;
1295
1296 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1297 int CppHashLocLineNo =
1298 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1299 int LineNo = Parser->CppHashLineNumber - 1 +
1300 (DiagLocLineNo - CppHashLocLineNo);
1301
1302 SMDiagnostic NewDiag(*Diag.getSourceMgr(),
1303 Diag.getLoc(),
1304 Filename,
1305 LineNo,
1306 Diag.getColumnNo(),
1307 Diag.getMessage(),
1308 Diag.getLineContents(),
1309 Diag.getShowLine());
1310
1311 NewDiag.Print(0, OS);
1312}
1313
Rafael Espindola65366442011-06-05 02:43:45 +00001314bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1315 const std::vector<StringRef> &Parameters,
1316 const std::vector<std::vector<AsmToken> > &A,
1317 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001318 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001319 unsigned NParameters = Parameters.size();
1320 if (NParameters != 0 && NParameters != A.size())
1321 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001322
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001323 while (!Body.empty()) {
1324 // Scan for the next substitution.
1325 std::size_t End = Body.size(), Pos = 0;
1326 for (; Pos != End; ++Pos) {
1327 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001328 if (!NParameters) {
1329 // This macro has no parameters, look for $0, $1, etc.
1330 if (Body[Pos] != '$' || Pos + 1 == End)
1331 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001332
Rafael Espindola65366442011-06-05 02:43:45 +00001333 char Next = Body[Pos + 1];
1334 if (Next == '$' || Next == 'n' || isdigit(Next))
1335 break;
1336 } else {
1337 // This macro has parameters, look for \foo, \bar, etc.
1338 if (Body[Pos] == '\\' && Pos + 1 != End)
1339 break;
1340 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001341 }
1342
1343 // Add the prefix.
1344 OS << Body.slice(0, Pos);
1345
1346 // Check if we reached the end.
1347 if (Pos == End)
1348 break;
1349
Rafael Espindola65366442011-06-05 02:43:45 +00001350 if (!NParameters) {
1351 switch (Body[Pos+1]) {
1352 // $$ => $
1353 case '$':
1354 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001355 break;
1356
Rafael Espindola65366442011-06-05 02:43:45 +00001357 // $n => number of arguments
1358 case 'n':
1359 OS << A.size();
1360 break;
1361
1362 // $[0-9] => argument
1363 default: {
1364 // Missing arguments are ignored.
1365 unsigned Index = Body[Pos+1] - '0';
1366 if (Index >= A.size())
1367 break;
1368
1369 // Otherwise substitute with the token values, with spaces eliminated.
1370 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1371 ie = A[Index].end(); it != ie; ++it)
1372 OS << it->getString();
1373 break;
1374 }
1375 }
1376 Pos += 2;
1377 } else {
1378 unsigned I = Pos + 1;
1379 while (isalnum(Body[I]) && I + 1 != End)
1380 ++I;
1381
1382 const char *Begin = Body.data() + Pos +1;
1383 StringRef Argument(Begin, I - (Pos +1));
1384 unsigned Index = 0;
1385 for (; Index < NParameters; ++Index)
1386 if (Parameters[Index] == Argument)
1387 break;
1388
1389 // FIXME: We should error at the macro definition.
1390 if (Index == NParameters)
1391 return Error(L, "Parameter not found");
1392
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001393 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1394 ie = A[Index].end(); it != ie; ++it)
1395 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001396
Rafael Espindola65366442011-06-05 02:43:45 +00001397 Pos += 1 + Argument.size();
1398 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001399 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001400 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001401 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001402
1403 // We include the .endmacro in the buffer as our queue to exit the macro
1404 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001405 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001406 return false;
1407}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001408
Rafael Espindola65366442011-06-05 02:43:45 +00001409MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1410 MemoryBuffer *I)
1411 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1412{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001413}
1414
1415bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1416 const Macro *M) {
1417 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1418 // this, although we should protect against infinite loops.
1419 if (ActiveMacros.size() == 20)
1420 return TokError("macros cannot be nested more than 20 levels deep");
1421
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001422 // Parse the macro instantiation arguments.
1423 std::vector<std::vector<AsmToken> > MacroArguments;
1424 MacroArguments.push_back(std::vector<AsmToken>());
1425 unsigned ParenLevel = 0;
1426 for (;;) {
1427 if (Lexer.is(AsmToken::Eof))
1428 return TokError("unexpected token in macro instantiation");
1429 if (Lexer.is(AsmToken::EndOfStatement))
1430 break;
1431
1432 // If we aren't inside parentheses and this is a comma, start a new token
1433 // list.
1434 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1435 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001436 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001437 // Adjust the current parentheses level.
1438 if (Lexer.is(AsmToken::LParen))
1439 ++ParenLevel;
1440 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1441 --ParenLevel;
1442
1443 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001444 MacroArguments.back().push_back(getTok());
1445 }
1446 Lex();
1447 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001448
Rafael Espindola65366442011-06-05 02:43:45 +00001449 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1450 // to hold the macro body with substitutions.
1451 SmallString<256> Buf;
1452 StringRef Body = M->Body;
1453
1454 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1455 return true;
1456
1457 MemoryBuffer *Instantiation =
1458 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1459
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001460 // Create the macro instantiation object and add to the current macro
1461 // instantiation stack.
1462 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001463 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001464 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001465 ActiveMacros.push_back(MI);
1466
1467 // Jump to the macro instantiation and prime the lexer.
1468 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1469 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1470 Lex();
1471
1472 return false;
1473}
1474
1475void AsmParser::HandleMacroExit() {
1476 // Jump to the EndOfStatement we should return to, and consume it.
1477 JumpToLoc(ActiveMacros.back()->ExitLoc);
1478 Lex();
1479
1480 // Pop the instantiation entry.
1481 delete ActiveMacros.back();
1482 ActiveMacros.pop_back();
1483}
1484
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001485static void MarkUsed(const MCExpr *Value) {
1486 switch (Value->getKind()) {
1487 case MCExpr::Binary:
1488 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1489 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1490 break;
1491 case MCExpr::Target:
1492 case MCExpr::Constant:
1493 break;
1494 case MCExpr::SymbolRef: {
1495 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1496 break;
1497 }
1498 case MCExpr::Unary:
1499 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1500 break;
1501 }
1502}
1503
Nico Weber4c4c7322011-01-28 03:04:41 +00001504bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001505 // FIXME: Use better location, we should use proper tokens.
1506 SMLoc EqualLoc = Lexer.getLoc();
1507
Daniel Dunbar821e3332009-08-31 08:09:28 +00001508 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001509 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001510 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001511
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001512 MarkUsed(Value);
1513
Daniel Dunbar3f872332009-07-28 16:08:33 +00001514 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001515 return TokError("unexpected token in assignment");
1516
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001517 // Error on assignment to '.'.
1518 if (Name == ".") {
1519 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1520 "(use '.space' or '.org').)"));
1521 }
1522
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001523 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001524 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001525
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001526 // Validate that the LHS is allowed to be a variable (either it has not been
1527 // used as a symbol, or it is an absolute symbol).
1528 MCSymbol *Sym = getContext().LookupSymbol(Name);
1529 if (Sym) {
1530 // Diagnose assignment to a label.
1531 //
1532 // FIXME: Diagnostics. Note the location of the definition as a label.
1533 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001534 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001535 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001536 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001537 return Error(EqualLoc, "redefinition of '" + Name + "'");
1538 else if (!Sym->isVariable())
1539 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001540 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001541 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1542 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001543
1544 // Don't count these checks as uses.
1545 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001546 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001547 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001548
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001549 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001550
1551 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001552 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001553
1554 return false;
1555}
1556
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001557/// ParseIdentifier:
1558/// ::= identifier
1559/// ::= string
1560bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001561 // The assembler has relaxed rules for accepting identifiers, in particular we
1562 // allow things like '.globl $foo', which would normally be separate
1563 // tokens. At this level, we have already lexed so we cannot (currently)
1564 // handle this as a context dependent token, instead we detect adjacent tokens
1565 // and return the combined identifier.
1566 if (Lexer.is(AsmToken::Dollar)) {
1567 SMLoc DollarLoc = getLexer().getLoc();
1568
1569 // Consume the dollar sign, and check for a following identifier.
1570 Lex();
1571 if (Lexer.isNot(AsmToken::Identifier))
1572 return true;
1573
1574 // We have a '$' followed by an identifier, make sure they are adjacent.
1575 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1576 return true;
1577
1578 // Construct the joined identifier and consume the token.
1579 Res = StringRef(DollarLoc.getPointer(),
1580 getTok().getIdentifier().size() + 1);
1581 Lex();
1582 return false;
1583 }
1584
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001585 if (Lexer.isNot(AsmToken::Identifier) &&
1586 Lexer.isNot(AsmToken::String))
1587 return true;
1588
Sean Callanan18b83232010-01-19 21:44:56 +00001589 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001590
Sean Callanan79ed1a82010-01-19 20:22:31 +00001591 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001592
1593 return false;
1594}
1595
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001596/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001597/// ::= .equ identifier ',' expression
1598/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001599/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001600bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001601 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001602
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001603 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001604 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001605
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001606 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001607 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001608 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001609
Nico Weber4c4c7322011-01-28 03:04:41 +00001610 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001611}
1612
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001613bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001614 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001615
1616 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001617 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001618 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1619 if (Str[i] != '\\') {
1620 Data += Str[i];
1621 continue;
1622 }
1623
1624 // Recognize escaped characters. Note that this escape semantics currently
1625 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1626 ++i;
1627 if (i == e)
1628 return TokError("unexpected backslash at end of string");
1629
1630 // Recognize octal sequences.
1631 if ((unsigned) (Str[i] - '0') <= 7) {
1632 // Consume up to three octal characters.
1633 unsigned Value = Str[i] - '0';
1634
1635 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1636 ++i;
1637 Value = Value * 8 + (Str[i] - '0');
1638
1639 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1640 ++i;
1641 Value = Value * 8 + (Str[i] - '0');
1642 }
1643 }
1644
1645 if (Value > 255)
1646 return TokError("invalid octal escape sequence (out of range)");
1647
1648 Data += (unsigned char) Value;
1649 continue;
1650 }
1651
1652 // Otherwise recognize individual escapes.
1653 switch (Str[i]) {
1654 default:
1655 // Just reject invalid escape sequences for now.
1656 return TokError("invalid escape sequence (unrecognized character)");
1657
1658 case 'b': Data += '\b'; break;
1659 case 'f': Data += '\f'; break;
1660 case 'n': Data += '\n'; break;
1661 case 'r': Data += '\r'; break;
1662 case 't': Data += '\t'; break;
1663 case '"': Data += '"'; break;
1664 case '\\': Data += '\\'; break;
1665 }
1666 }
1667
1668 return false;
1669}
1670
Daniel Dunbara0d14262009-06-24 23:30:00 +00001671/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001672/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1673bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001674 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001675 CheckForValidSection();
1676
Daniel Dunbara0d14262009-06-24 23:30:00 +00001677 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001678 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001679 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001680
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001681 std::string Data;
1682 if (ParseEscapedString(Data))
1683 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001684
1685 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001686 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001687 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1688
Sean Callanan79ed1a82010-01-19 20:22:31 +00001689 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001690
1691 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001692 break;
1693
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001694 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001695 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001696 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001697 }
1698 }
1699
Sean Callanan79ed1a82010-01-19 20:22:31 +00001700 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001701 return false;
1702}
1703
1704/// ParseDirectiveValue
1705/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1706bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001707 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001708 CheckForValidSection();
1709
Daniel Dunbara0d14262009-06-24 23:30:00 +00001710 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001711 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001712 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001713 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001714 return true;
1715
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001716 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001717 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1718 assert(Size <= 8 && "Invalid size");
1719 uint64_t IntValue = MCE->getValue();
1720 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1721 return Error(ExprLoc, "literal value out of range for directive");
1722 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1723 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001724 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001725
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001726 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001727 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001728
Daniel Dunbara0d14262009-06-24 23:30:00 +00001729 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001730 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001731 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001732 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001733 }
1734 }
1735
Sean Callanan79ed1a82010-01-19 20:22:31 +00001736 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001737 return false;
1738}
1739
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001740/// ParseDirectiveRealValue
1741/// ::= (.single | .double) [ expression (, expression)* ]
1742bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1743 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1744 CheckForValidSection();
1745
1746 for (;;) {
1747 // We don't truly support arithmetic on floating point expressions, so we
1748 // have to manually parse unary prefixes.
1749 bool IsNeg = false;
1750 if (getLexer().is(AsmToken::Minus)) {
1751 Lex();
1752 IsNeg = true;
1753 } else if (getLexer().is(AsmToken::Plus))
1754 Lex();
1755
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001756 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001757 getLexer().isNot(AsmToken::Real) &&
1758 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001759 return TokError("unexpected token in directive");
1760
1761 // Convert to an APFloat.
1762 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001763 StringRef IDVal = getTok().getString();
1764 if (getLexer().is(AsmToken::Identifier)) {
1765 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1766 Value = APFloat::getInf(Semantics);
1767 else if (!IDVal.compare_lower("nan"))
1768 Value = APFloat::getNaN(Semantics, false, ~0);
1769 else
1770 return TokError("invalid floating point literal");
1771 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001772 APFloat::opInvalidOp)
1773 return TokError("invalid floating point literal");
1774 if (IsNeg)
1775 Value.changeSign();
1776
1777 // Consume the numeric token.
1778 Lex();
1779
1780 // Emit the value as an integer.
1781 APInt AsInt = Value.bitcastToAPInt();
1782 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1783 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1784
1785 if (getLexer().is(AsmToken::EndOfStatement))
1786 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001787
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001788 if (getLexer().isNot(AsmToken::Comma))
1789 return TokError("unexpected token in directive");
1790 Lex();
1791 }
1792 }
1793
1794 Lex();
1795 return false;
1796}
1797
Daniel Dunbara0d14262009-06-24 23:30:00 +00001798/// ParseDirectiveSpace
1799/// ::= .space expression [ , expression ]
1800bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001801 CheckForValidSection();
1802
Daniel Dunbara0d14262009-06-24 23:30:00 +00001803 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001804 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001805 return true;
1806
1807 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001808 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1809 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001810 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001811 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001812
Daniel Dunbar475839e2009-06-29 20:37:27 +00001813 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001814 return true;
1815
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001816 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001817 return TokError("unexpected token in '.space' directive");
1818 }
1819
Sean Callanan79ed1a82010-01-19 20:22:31 +00001820 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001821
1822 if (NumBytes <= 0)
1823 return TokError("invalid number of bytes in '.space' directive");
1824
1825 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001826 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001827
1828 return false;
1829}
1830
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001831/// ParseDirectiveZero
1832/// ::= .zero expression
1833bool AsmParser::ParseDirectiveZero() {
1834 CheckForValidSection();
1835
1836 int64_t NumBytes;
1837 if (ParseAbsoluteExpression(NumBytes))
1838 return true;
1839
Rafael Espindolae452b172010-10-05 19:42:57 +00001840 int64_t Val = 0;
1841 if (getLexer().is(AsmToken::Comma)) {
1842 Lex();
1843 if (ParseAbsoluteExpression(Val))
1844 return true;
1845 }
1846
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001847 if (getLexer().isNot(AsmToken::EndOfStatement))
1848 return TokError("unexpected token in '.zero' directive");
1849
1850 Lex();
1851
Rafael Espindolae452b172010-10-05 19:42:57 +00001852 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001853
1854 return false;
1855}
1856
Daniel Dunbara0d14262009-06-24 23:30:00 +00001857/// ParseDirectiveFill
1858/// ::= .fill expression , expression , expression
1859bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001860 CheckForValidSection();
1861
Daniel Dunbara0d14262009-06-24 23:30:00 +00001862 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001863 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001864 return true;
1865
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001866 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001867 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001868 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001869
Daniel Dunbara0d14262009-06-24 23:30:00 +00001870 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001871 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001872 return true;
1873
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001874 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001875 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001876 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001877
Daniel Dunbara0d14262009-06-24 23:30:00 +00001878 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001879 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001880 return true;
1881
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001882 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001883 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001884
Sean Callanan79ed1a82010-01-19 20:22:31 +00001885 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001886
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001887 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1888 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001889
1890 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001891 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001892
1893 return false;
1894}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001895
1896/// ParseDirectiveOrg
1897/// ::= .org expression [ , expression ]
1898bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001899 CheckForValidSection();
1900
Daniel Dunbar821e3332009-08-31 08:09:28 +00001901 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001902 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001903 return true;
1904
1905 // Parse optional fill expression.
1906 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001907 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1908 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001909 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001910 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001911
Daniel Dunbar475839e2009-06-29 20:37:27 +00001912 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001913 return true;
1914
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001915 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001916 return TokError("unexpected token in '.org' directive");
1917 }
1918
Sean Callanan79ed1a82010-01-19 20:22:31 +00001919 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001920
1921 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1922 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001923 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001924
1925 return false;
1926}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001927
1928/// ParseDirectiveAlign
1929/// ::= {.align, ...} expression [ , expression [ , expression ]]
1930bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001931 CheckForValidSection();
1932
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001933 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001934 int64_t Alignment;
1935 if (ParseAbsoluteExpression(Alignment))
1936 return true;
1937
1938 SMLoc MaxBytesLoc;
1939 bool HasFillExpr = false;
1940 int64_t FillExpr = 0;
1941 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001942 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1943 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001944 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001945 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001946
1947 // The fill expression can be omitted while specifying a maximum number of
1948 // alignment bytes, e.g:
1949 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001950 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001951 HasFillExpr = true;
1952 if (ParseAbsoluteExpression(FillExpr))
1953 return true;
1954 }
1955
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001956 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1957 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001958 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001959 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001960
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001961 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001962 if (ParseAbsoluteExpression(MaxBytesToFill))
1963 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001964
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001965 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001966 return TokError("unexpected token in directive");
1967 }
1968 }
1969
Sean Callanan79ed1a82010-01-19 20:22:31 +00001970 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001971
Daniel Dunbar648ac512010-05-17 21:54:30 +00001972 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001973 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001974
1975 // Compute alignment in bytes.
1976 if (IsPow2) {
1977 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001978 if (Alignment >= 32) {
1979 Error(AlignmentLoc, "invalid alignment value");
1980 Alignment = 31;
1981 }
1982
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001983 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001984 }
1985
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001986 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001987 if (MaxBytesLoc.isValid()) {
1988 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001989 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1990 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001991 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001992 }
1993
1994 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001995 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1996 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001997 MaxBytesToFill = 0;
1998 }
1999 }
2000
Daniel Dunbar648ac512010-05-17 21:54:30 +00002001 // Check whether we should use optimal code alignment for this .align
2002 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002003 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002004 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2005 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002006 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002007 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002008 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002009 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2010 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002011 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002012
2013 return false;
2014}
2015
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002016/// ParseDirectiveSymbolAttribute
2017/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002018bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002019 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002020 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002021 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002022 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002023
2024 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002025 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002026
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002027 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002028
Jim Grosbach10ec6502011-09-15 17:56:49 +00002029 // Assembler local symbols don't make any sense here. Complain loudly.
2030 if (Sym->isTemporary())
2031 return Error(Loc, "non-local symbol required in directive");
2032
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002033 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002034
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002035 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002036 break;
2037
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002038 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002039 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002040 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002041 }
2042 }
2043
Sean Callanan79ed1a82010-01-19 20:22:31 +00002044 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002045 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002046}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002047
2048/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002049/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2050bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002051 CheckForValidSection();
2052
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002053 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002054 StringRef Name;
2055 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002056 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002057
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002058 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002059 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002060
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002061 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002062 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002063 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002064
2065 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002066 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002067 if (ParseAbsoluteExpression(Size))
2068 return true;
2069
2070 int64_t Pow2Alignment = 0;
2071 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002072 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002073 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002074 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002075 if (ParseAbsoluteExpression(Pow2Alignment))
2076 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002077
Chris Lattner258281d2010-01-19 06:22:22 +00002078 // If this target takes alignments in bytes (not log) validate and convert.
2079 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2080 if (!isPowerOf2_64(Pow2Alignment))
2081 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2082 Pow2Alignment = Log2_64(Pow2Alignment);
2083 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002084 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002085
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002086 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002087 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002088
Sean Callanan79ed1a82010-01-19 20:22:31 +00002089 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002090
Chris Lattner1fc3d752009-07-09 17:25:12 +00002091 // NOTE: a size of zero for a .comm should create a undefined symbol
2092 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002093 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002094 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2095 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002096
Eric Christopherc260a3e2010-05-14 01:38:54 +00002097 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002098 // may internally end up wanting an alignment in bytes.
2099 // FIXME: Diagnose overflow.
2100 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002101 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2102 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002103
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002104 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002105 return Error(IDLoc, "invalid symbol redefinition");
2106
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002107 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002108 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002109 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002110 getStreamer().EmitZerofill(Ctx.getMachOSection(
2111 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2112 0, SectionKind::getBSS()),
2113 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002114 return false;
2115 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002116
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002117 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002118 return false;
2119}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002120
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002121/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002122/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002123bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002124 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002125 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002126
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002127 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002128 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002129 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002130
Sean Callanan79ed1a82010-01-19 20:22:31 +00002131 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002132
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002133 if (Str.empty())
2134 Error(Loc, ".abort detected. Assembly stopping.");
2135 else
2136 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002137 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002138
2139 return false;
2140}
Kevin Enderby71148242009-07-14 21:35:03 +00002141
Kevin Enderby1f049b22009-07-14 23:21:55 +00002142/// ParseDirectiveInclude
2143/// ::= .include "filename"
2144bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002145 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002146 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002147
Sean Callanan18b83232010-01-19 21:44:56 +00002148 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002149 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002150 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002151
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002152 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002153 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002154
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002155 // Strip the quotes.
2156 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002157
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002158 // Attempt to switch the lexer to the included file before consuming the end
2159 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002160 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002161 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002162 return true;
2163 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002164
2165 return false;
2166}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002167
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002168/// ParseDirectiveIf
2169/// ::= .if expression
2170bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002171 TheCondStack.push_back(TheCondState);
2172 TheCondState.TheCond = AsmCond::IfCond;
2173 if(TheCondState.Ignore) {
2174 EatToEndOfStatement();
2175 }
2176 else {
2177 int64_t ExprValue;
2178 if (ParseAbsoluteExpression(ExprValue))
2179 return true;
2180
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002181 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002182 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002183
Sean Callanan79ed1a82010-01-19 20:22:31 +00002184 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002185
2186 TheCondState.CondMet = ExprValue;
2187 TheCondState.Ignore = !TheCondState.CondMet;
2188 }
2189
2190 return false;
2191}
2192
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002193bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2194 StringRef Name;
2195 TheCondStack.push_back(TheCondState);
2196 TheCondState.TheCond = AsmCond::IfCond;
2197
2198 if (TheCondState.Ignore) {
2199 EatToEndOfStatement();
2200 } else {
2201 if (ParseIdentifier(Name))
2202 return TokError("expected identifier after '.ifdef'");
2203
2204 Lex();
2205
2206 MCSymbol *Sym = getContext().LookupSymbol(Name);
2207
2208 if (expect_defined)
2209 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2210 else
2211 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2212 TheCondState.Ignore = !TheCondState.CondMet;
2213 }
2214
2215 return false;
2216}
2217
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002218/// ParseDirectiveElseIf
2219/// ::= .elseif expression
2220bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2221 if (TheCondState.TheCond != AsmCond::IfCond &&
2222 TheCondState.TheCond != AsmCond::ElseIfCond)
2223 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2224 " an .elseif");
2225 TheCondState.TheCond = AsmCond::ElseIfCond;
2226
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002227 bool LastIgnoreState = false;
2228 if (!TheCondStack.empty())
2229 LastIgnoreState = TheCondStack.back().Ignore;
2230 if (LastIgnoreState || TheCondState.CondMet) {
2231 TheCondState.Ignore = true;
2232 EatToEndOfStatement();
2233 }
2234 else {
2235 int64_t ExprValue;
2236 if (ParseAbsoluteExpression(ExprValue))
2237 return true;
2238
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002239 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002240 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002241
Sean Callanan79ed1a82010-01-19 20:22:31 +00002242 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002243 TheCondState.CondMet = ExprValue;
2244 TheCondState.Ignore = !TheCondState.CondMet;
2245 }
2246
2247 return false;
2248}
2249
2250/// ParseDirectiveElse
2251/// ::= .else
2252bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002253 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002254 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002255
Sean Callanan79ed1a82010-01-19 20:22:31 +00002256 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002257
2258 if (TheCondState.TheCond != AsmCond::IfCond &&
2259 TheCondState.TheCond != AsmCond::ElseIfCond)
2260 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2261 ".elseif");
2262 TheCondState.TheCond = AsmCond::ElseCond;
2263 bool LastIgnoreState = false;
2264 if (!TheCondStack.empty())
2265 LastIgnoreState = TheCondStack.back().Ignore;
2266 if (LastIgnoreState || TheCondState.CondMet)
2267 TheCondState.Ignore = true;
2268 else
2269 TheCondState.Ignore = false;
2270
2271 return false;
2272}
2273
2274/// ParseDirectiveEndIf
2275/// ::= .endif
2276bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002277 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002278 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002279
Sean Callanan79ed1a82010-01-19 20:22:31 +00002280 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002281
2282 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2283 TheCondStack.empty())
2284 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2285 ".else");
2286 if (!TheCondStack.empty()) {
2287 TheCondState = TheCondStack.back();
2288 TheCondStack.pop_back();
2289 }
2290
2291 return false;
2292}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002293
2294/// ParseDirectiveFile
2295/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002296bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002297 // FIXME: I'm not sure what this is.
2298 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002299 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002300 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002301 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002302 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002303
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002304 if (FileNumber < 1)
2305 return TokError("file number less than one");
2306 }
2307
Daniel Dunbareceec052010-07-12 17:45:27 +00002308 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002309 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002310
Chris Lattnerd32e8032010-01-25 19:02:58 +00002311 StringRef Filename = getTok().getString();
2312 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002313 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002314
Daniel Dunbareceec052010-07-12 17:45:27 +00002315 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002316 return TokError("unexpected token in '.file' directive");
2317
Chris Lattnerd32e8032010-01-25 19:02:58 +00002318 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002319 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002320 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002321 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002322 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002323 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002324
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002325 return false;
2326}
2327
2328/// ParseDirectiveLine
2329/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002330bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002331 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2332 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002333 return TokError("unexpected token in '.line' directive");
2334
Sean Callanan18b83232010-01-19 21:44:56 +00002335 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002336 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002337 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002338
2339 // FIXME: Do something with the .line.
2340 }
2341
Daniel Dunbareceec052010-07-12 17:45:27 +00002342 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002343 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002344
2345 return false;
2346}
2347
2348
2349/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002350/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002351/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2352/// The first number is a file number, must have been previously assigned with
2353/// a .file directive, the second number is the line number and optionally the
2354/// third number is a column position (zero if not specified). The remaining
2355/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002356bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002357
Daniel Dunbareceec052010-07-12 17:45:27 +00002358 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002359 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002360 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002361 if (FileNumber < 1)
2362 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002363 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002364 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002365 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002366
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002367 int64_t LineNumber = 0;
2368 if (getLexer().is(AsmToken::Integer)) {
2369 LineNumber = getTok().getIntVal();
2370 if (LineNumber < 1)
2371 return TokError("line number less than one in '.loc' directive");
2372 Lex();
2373 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002374
2375 int64_t ColumnPos = 0;
2376 if (getLexer().is(AsmToken::Integer)) {
2377 ColumnPos = getTok().getIntVal();
2378 if (ColumnPos < 0)
2379 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002380 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002381 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002382
Kevin Enderbyc0957932010-09-30 16:52:03 +00002383 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002384 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002385 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002386 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2387 for (;;) {
2388 if (getLexer().is(AsmToken::EndOfStatement))
2389 break;
2390
2391 StringRef Name;
2392 SMLoc Loc = getTok().getLoc();
2393 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002394 return TokError("unexpected token in '.loc' directive");
2395
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002396 if (Name == "basic_block")
2397 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2398 else if (Name == "prologue_end")
2399 Flags |= DWARF2_FLAG_PROLOGUE_END;
2400 else if (Name == "epilogue_begin")
2401 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2402 else if (Name == "is_stmt") {
2403 SMLoc Loc = getTok().getLoc();
2404 const MCExpr *Value;
2405 if (getParser().ParseExpression(Value))
2406 return true;
2407 // The expression must be the constant 0 or 1.
2408 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2409 int Value = MCE->getValue();
2410 if (Value == 0)
2411 Flags &= ~DWARF2_FLAG_IS_STMT;
2412 else if (Value == 1)
2413 Flags |= DWARF2_FLAG_IS_STMT;
2414 else
2415 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002416 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002417 else {
2418 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2419 }
2420 }
2421 else if (Name == "isa") {
2422 SMLoc Loc = getTok().getLoc();
2423 const MCExpr *Value;
2424 if (getParser().ParseExpression(Value))
2425 return true;
2426 // The expression must be a constant greater or equal to 0.
2427 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2428 int Value = MCE->getValue();
2429 if (Value < 0)
2430 return Error(Loc, "isa number less than zero");
2431 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002432 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002433 else {
2434 return Error(Loc, "isa number not a constant value");
2435 }
2436 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002437 else if (Name == "discriminator") {
2438 if (getParser().ParseAbsoluteExpression(Discriminator))
2439 return true;
2440 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002441 else {
2442 return Error(Loc, "unknown sub-directive in '.loc' directive");
2443 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002444
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002445 if (getLexer().is(AsmToken::EndOfStatement))
2446 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002447 }
2448 }
2449
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002450 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002451 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002452
2453 return false;
2454}
2455
Daniel Dunbar138abae2010-10-16 04:56:42 +00002456/// ParseDirectiveStabs
2457/// ::= .stabs string, number, number, number
2458bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2459 SMLoc DirectiveLoc) {
2460 return TokError("unsupported directive '" + Directive + "'");
2461}
2462
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002463/// ParseDirectiveCFISections
2464/// ::= .cfi_sections section [, section]
2465bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2466 SMLoc DirectiveLoc) {
2467 StringRef Name;
2468 bool EH = false;
2469 bool Debug = false;
2470
2471 if (getParser().ParseIdentifier(Name))
2472 return TokError("Expected an identifier");
2473
2474 if (Name == ".eh_frame")
2475 EH = true;
2476 else if (Name == ".debug_frame")
2477 Debug = true;
2478
2479 if (getLexer().is(AsmToken::Comma)) {
2480 Lex();
2481
2482 if (getParser().ParseIdentifier(Name))
2483 return TokError("Expected an identifier");
2484
2485 if (Name == ".eh_frame")
2486 EH = true;
2487 else if (Name == ".debug_frame")
2488 Debug = true;
2489 }
2490
2491 getStreamer().EmitCFISections(EH, Debug);
2492
2493 return false;
2494}
2495
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002496/// ParseDirectiveCFIStartProc
2497/// ::= .cfi_startproc
2498bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2499 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002500 getStreamer().EmitCFIStartProc();
2501 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002502}
2503
2504/// ParseDirectiveCFIEndProc
2505/// ::= .cfi_endproc
2506bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002507 getStreamer().EmitCFIEndProc();
2508 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002509}
2510
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002511/// ParseRegisterOrRegisterNumber - parse register name or number.
2512bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2513 SMLoc DirectiveLoc) {
2514 unsigned RegNo;
2515
Jim Grosbach6f888a82011-06-02 17:14:04 +00002516 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002517 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2518 DirectiveLoc))
2519 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002520 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002521 } else
2522 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002523
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002524 return false;
2525}
2526
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002527/// ParseDirectiveCFIDefCfa
2528/// ::= .cfi_def_cfa register, offset
2529bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2530 SMLoc DirectiveLoc) {
2531 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002532 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002533 return true;
2534
2535 if (getLexer().isNot(AsmToken::Comma))
2536 return TokError("unexpected token in directive");
2537 Lex();
2538
2539 int64_t Offset = 0;
2540 if (getParser().ParseAbsoluteExpression(Offset))
2541 return true;
2542
Rafael Espindola066c2f42011-04-12 23:59:07 +00002543 getStreamer().EmitCFIDefCfa(Register, Offset);
2544 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002545}
2546
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002547/// ParseDirectiveCFIDefCfaOffset
2548/// ::= .cfi_def_cfa_offset offset
2549bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2550 SMLoc DirectiveLoc) {
2551 int64_t Offset = 0;
2552 if (getParser().ParseAbsoluteExpression(Offset))
2553 return true;
2554
Rafael Espindola066c2f42011-04-12 23:59:07 +00002555 getStreamer().EmitCFIDefCfaOffset(Offset);
2556 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002557}
2558
2559/// ParseDirectiveCFIAdjustCfaOffset
2560/// ::= .cfi_adjust_cfa_offset adjustment
2561bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2562 SMLoc DirectiveLoc) {
2563 int64_t Adjustment = 0;
2564 if (getParser().ParseAbsoluteExpression(Adjustment))
2565 return true;
2566
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002567 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2568 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002569}
2570
2571/// ParseDirectiveCFIDefCfaRegister
2572/// ::= .cfi_def_cfa_register register
2573bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2574 SMLoc DirectiveLoc) {
2575 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002576 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002577 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002578
Rafael Espindola066c2f42011-04-12 23:59:07 +00002579 getStreamer().EmitCFIDefCfaRegister(Register);
2580 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002581}
2582
2583/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002584/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002585bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2586 int64_t Register = 0;
2587 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002588
2589 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002590 return true;
2591
2592 if (getLexer().isNot(AsmToken::Comma))
2593 return TokError("unexpected token in directive");
2594 Lex();
2595
2596 if (getParser().ParseAbsoluteExpression(Offset))
2597 return true;
2598
Rafael Espindola066c2f42011-04-12 23:59:07 +00002599 getStreamer().EmitCFIOffset(Register, Offset);
2600 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002601}
2602
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002603/// ParseDirectiveCFIRelOffset
2604/// ::= .cfi_rel_offset register, offset
2605bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2606 SMLoc DirectiveLoc) {
2607 int64_t Register = 0;
2608
2609 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2610 return true;
2611
2612 if (getLexer().isNot(AsmToken::Comma))
2613 return TokError("unexpected token in directive");
2614 Lex();
2615
2616 int64_t Offset = 0;
2617 if (getParser().ParseAbsoluteExpression(Offset))
2618 return true;
2619
Rafael Espindola25f492e2011-04-12 16:12:03 +00002620 getStreamer().EmitCFIRelOffset(Register, Offset);
2621 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002622}
2623
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002624static bool isValidEncoding(int64_t Encoding) {
2625 if (Encoding & ~0xff)
2626 return false;
2627
2628 if (Encoding == dwarf::DW_EH_PE_omit)
2629 return true;
2630
2631 const unsigned Format = Encoding & 0xf;
2632 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2633 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2634 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2635 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2636 return false;
2637
Rafael Espindolacaf11582010-12-29 04:31:26 +00002638 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002639 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002640 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002641 return false;
2642
2643 return true;
2644}
2645
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002646/// ParseDirectiveCFIPersonalityOrLsda
2647/// ::= .cfi_personality encoding, [symbol_name]
2648/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002649bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002650 SMLoc DirectiveLoc) {
2651 int64_t Encoding = 0;
2652 if (getParser().ParseAbsoluteExpression(Encoding))
2653 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002654 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002655 return false;
2656
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002657 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002658 return TokError("unsupported encoding.");
2659
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002660 if (getLexer().isNot(AsmToken::Comma))
2661 return TokError("unexpected token in directive");
2662 Lex();
2663
2664 StringRef Name;
2665 if (getParser().ParseIdentifier(Name))
2666 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002667
2668 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2669
2670 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002671 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002672 else {
2673 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002674 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002675 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002676 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002677}
2678
Rafael Espindolafe024d02010-12-28 18:36:23 +00002679/// ParseDirectiveCFIRememberState
2680/// ::= .cfi_remember_state
2681bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2682 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002683 getStreamer().EmitCFIRememberState();
2684 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002685}
2686
2687/// ParseDirectiveCFIRestoreState
2688/// ::= .cfi_remember_state
2689bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2690 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002691 getStreamer().EmitCFIRestoreState();
2692 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002693}
2694
Rafael Espindolac5754392011-04-12 15:31:05 +00002695/// ParseDirectiveCFISameValue
2696/// ::= .cfi_same_value register
2697bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2698 SMLoc DirectiveLoc) {
2699 int64_t Register = 0;
2700
2701 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2702 return true;
2703
2704 getStreamer().EmitCFISameValue(Register);
2705
2706 return false;
2707}
2708
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002709/// ParseDirectiveMacrosOnOff
2710/// ::= .macros_on
2711/// ::= .macros_off
2712bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2713 SMLoc DirectiveLoc) {
2714 if (getLexer().isNot(AsmToken::EndOfStatement))
2715 return Error(getLexer().getLoc(),
2716 "unexpected token in '" + Directive + "' directive");
2717
2718 getParser().MacrosEnabled = Directive == ".macros_on";
2719
2720 return false;
2721}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002722
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002723/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002724/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002725bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2726 SMLoc DirectiveLoc) {
2727 StringRef Name;
2728 if (getParser().ParseIdentifier(Name))
2729 return TokError("expected identifier in directive");
2730
Rafael Espindola65366442011-06-05 02:43:45 +00002731 std::vector<StringRef> Parameters;
2732 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2733 for(;;) {
2734 StringRef Parameter;
2735 if (getParser().ParseIdentifier(Parameter))
2736 return TokError("expected identifier in directive");
2737 Parameters.push_back(Parameter);
2738
2739 if (getLexer().isNot(AsmToken::Comma))
2740 break;
2741 Lex();
2742 }
2743 }
2744
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002745 if (getLexer().isNot(AsmToken::EndOfStatement))
2746 return TokError("unexpected token in '.macro' directive");
2747
2748 // Eat the end of statement.
2749 Lex();
2750
2751 AsmToken EndToken, StartToken = getTok();
2752
2753 // Lex the macro definition.
2754 for (;;) {
2755 // Check whether we have reached the end of the file.
2756 if (getLexer().is(AsmToken::Eof))
2757 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2758
2759 // Otherwise, check whether we have reach the .endmacro.
2760 if (getLexer().is(AsmToken::Identifier) &&
2761 (getTok().getIdentifier() == ".endm" ||
2762 getTok().getIdentifier() == ".endmacro")) {
2763 EndToken = getTok();
2764 Lex();
2765 if (getLexer().isNot(AsmToken::EndOfStatement))
2766 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2767 "' directive");
2768 break;
2769 }
2770
2771 // Otherwise, scan til the end of the statement.
2772 getParser().EatToEndOfStatement();
2773 }
2774
2775 if (getParser().MacroMap.lookup(Name)) {
2776 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2777 }
2778
2779 const char *BodyStart = StartToken.getLoc().getPointer();
2780 const char *BodyEnd = EndToken.getLoc().getPointer();
2781 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002782 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002783 return false;
2784}
2785
2786/// ParseDirectiveEndMacro
2787/// ::= .endm
2788/// ::= .endmacro
2789bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2790 SMLoc DirectiveLoc) {
2791 if (getLexer().isNot(AsmToken::EndOfStatement))
2792 return TokError("unexpected token in '" + Directive + "' directive");
2793
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002794 // If we are inside a macro instantiation, terminate the current
2795 // instantiation.
2796 if (!getParser().ActiveMacros.empty()) {
2797 getParser().HandleMacroExit();
2798 return false;
2799 }
2800
2801 // Otherwise, this .endmacro is a stray entry in the file; well formed
2802 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002803 return TokError("unexpected '" + Directive + "' in file, "
2804 "no current macro definition");
2805}
2806
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002807bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002808 getParser().CheckForValidSection();
2809
2810 const MCExpr *Value;
2811
2812 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002813 return true;
2814
2815 if (getLexer().isNot(AsmToken::EndOfStatement))
2816 return TokError("unexpected token in directive");
2817
2818 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002819 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002820 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002821 getStreamer().EmitULEB128Value(Value);
2822
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002823 return false;
2824}
2825
2826
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002827/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002828MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002829 MCContext &C, MCStreamer &Out,
2830 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002831 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002832}