blob: b0491e79fe8b7d7284526f90adae0458f44df42a [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"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000020#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000026#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000027#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000028#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000029#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000030#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000031#include "llvm/Support/CommandLine.h"
Benjamin Kramer518ff562012-01-28 15:28:41 +000032#include "llvm/Support/ErrorHandling.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;
Benjamin Kramer04a04262011-10-16 10:48:29 +000090 SourceMgr::DiagHandlerTy SavedDiagHandler;
91 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000092 MCAsmParserExtension *GenericParser;
93 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000094
Daniel Dunbaraef87e32010-07-18 18:31:38 +000095 /// This is the current buffer index we're lexing from as managed by the
96 /// SourceMgr object.
97 int CurBuffer;
98
99 AsmCond TheCondState;
100 std::vector<AsmCond> TheCondStack;
101
102 /// DirectiveMap - This is a table handlers for directives. Each handler is
103 /// invoked after the directive identifier is read and is responsible for
104 /// parsing and validating the rest of the directive. The handler is passed
105 /// in the directive name and the location of the directive keyword.
106 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000107
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000108 /// MacroMap - Map of currently defined macros.
109 StringMap<Macro*> MacroMap;
110
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000111 /// ActiveMacros - Stack of active macro instantiations.
112 std::vector<MacroInstantiation*> ActiveMacros;
113
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000114 /// Boolean tracking whether macro substitution is enabled.
115 unsigned MacrosEnabled : 1;
116
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000117 /// Flag tracking whether any errors have been encountered.
118 unsigned HadError : 1;
119
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000120 /// The values from the last parsed cpp hash file line comment if any.
121 StringRef CppHashFilename;
122 int64_t CppHashLineNumber;
123 SMLoc CppHashLoc;
124
Devang Patel0db58bf2012-01-31 18:14:05 +0000125 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
126 unsigned AssemblerDialect;
127
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000128public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000129 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000130 const MCAsmInfo &MAI);
131 ~AsmParser();
132
133 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
134
135 void AddDirectiveHandler(MCAsmParserExtension *Object,
136 StringRef Directive,
137 DirectiveHandler Handler) {
138 DirectiveMap[Directive] = std::make_pair(Object, Handler);
139 }
140
141public:
142 /// @name MCAsmParser Interface
143 /// {
144
145 virtual SourceMgr &getSourceManager() { return SrcMgr; }
146 virtual MCAsmLexer &getLexer() { return Lexer; }
147 virtual MCContext &getContext() { return Ctx; }
148 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000149 virtual unsigned getAssemblerDialect() {
150 if (AssemblerDialect == ~0U)
151 return MAI.getAssemblerDialect();
152 else
153 return AssemblerDialect;
154 }
155 virtual void setAssemblerDialect(unsigned i) {
156 AssemblerDialect = i;
157 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000158
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000159 virtual bool Warning(SMLoc L, const Twine &Msg,
160 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
161 virtual bool Error(SMLoc L, const Twine &Msg,
162 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163
164 const AsmToken &Lex();
165
166 bool ParseExpression(const MCExpr *&Res);
167 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
168 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
169 virtual bool ParseAbsoluteExpression(int64_t &Res);
170
171 /// }
172
173private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000174 void CheckForValidSection();
175
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000176 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000177 void EatToEndOfLine();
178 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000179
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000180 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000181 bool expandMacro(SmallString<256> &Buf, StringRef Body,
182 const std::vector<StringRef> &Parameters,
183 const std::vector<std::vector<AsmToken> > &A,
184 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000185 void HandleMacroExit();
186
187 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000188 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000189 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
190 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000191 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000192 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000193
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000194 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
195 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000196 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
197 /// This returns true on failure.
198 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000199
200 /// \brief Reset the current lexer position to that given by \arg Loc. The
201 /// current token is not set; clients should ensure Lex() is called
202 /// subsequently.
203 void JumpToLoc(SMLoc Loc);
204
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000205 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000206
207 /// \brief Parse up to the end of statement and a return the contents from the
208 /// current token until the end of the statement; the current token on exit
209 /// will be either the EndOfStatement or EOF.
210 StringRef ParseStringToEndOfStatement();
211
Nico Weber4c4c7322011-01-28 03:04:41 +0000212 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000213
214 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
215 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
216 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000217 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000218
219 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
220 /// and set \arg Res to the identifier contents.
221 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000222
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000223 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000224
225 // ".ascii", ".asciiz", ".string"
226 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000227 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000228 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000229 bool ParseDirectiveFill(); // ".fill"
230 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000231 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000232 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000233 bool ParseDirectiveOrg(); // ".org"
234 // ".align{,32}", ".p2align{,w,l}"
235 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
236
237 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
238 /// accepts a single symbol (which should be a label or an external).
239 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000240
241 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
242
243 bool ParseDirectiveAbort(); // ".abort"
244 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000245 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000246
247 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000248 // ".ifb" or ".ifnb", depending on ExpectBlank.
249 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000250 // ".ifdef" or ".ifndef", depending on expect_defined
251 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000252 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
253 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
254 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
255
256 /// ParseEscapedString - Parse the current token as a string which may include
257 /// escaped characters and return the string contents.
258 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000259
260 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
261 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000262};
263
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000264/// \brief Generic implementations of directive handling, etc. which is shared
265/// (or the default, at least) for all assembler parser.
266class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000267 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
268 void AddDirectiveHandler(StringRef Directive) {
269 getParser().AddDirectiveHandler(this, Directive,
270 HandleDirective<GenericAsmParser, Handler>);
271 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000272public:
273 GenericAsmParser() {}
274
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000275 AsmParser &getParser() {
276 return (AsmParser&) this->MCAsmParserExtension::getParser();
277 }
278
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000279 virtual void Initialize(MCAsmParser &Parser) {
280 // Call the base implementation.
281 this->MCAsmParserExtension::Initialize(Parser);
282
283 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000284 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
285 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
286 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000287 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000288
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000289 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000290 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
291 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000292 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
293 ".cfi_startproc");
294 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
295 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000296 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
297 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000298 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
299 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000300 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
301 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000302 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
303 ".cfi_def_cfa_register");
304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
305 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
307 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000308 AddDirectiveHandler<
309 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
310 AddDirectiveHandler<
311 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000312 AddDirectiveHandler<
313 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
314 AddDirectiveHandler<
315 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000316 AddDirectiveHandler<
317 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000318 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000319 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
320 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000321 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000322 AddDirectiveHandler<
323 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000324
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000325 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000326 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
327 ".macros_on");
328 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
329 ".macros_off");
330 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
331 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
332 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000333
334 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
335 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000336 }
337
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000338 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
339
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000340 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
341 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
342 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000343 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000344 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000345 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
346 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000347 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000348 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000349 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000350 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
351 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000352 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000353 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000354 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
355 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000356 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000357 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000358 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000359 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000360
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000361 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000362 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
363 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000364
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000365 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000366};
367
368}
369
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000370namespace llvm {
371
372extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000373extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000374extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000375
376}
377
Chris Lattneraaec2052010-01-19 19:46:13 +0000378enum { DEFAULT_ADDRSPACE = 0 };
379
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000380AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000381 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000382 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000383 GenericParser(new GenericAsmParser), PlatformParser(0),
Devang Patel0db58bf2012-01-31 18:14:05 +0000384 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
385 AssemblerDialect(~0U) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000386 // Save the old handler.
387 SavedDiagHandler = SrcMgr.getDiagHandler();
388 SavedDiagContext = SrcMgr.getDiagContext();
389 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000390 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000391 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000392
393 // Initialize the generic parser.
394 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000395
396 // Initialize the platform / file format parser.
397 //
398 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
399 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000400 if (_MAI.hasMicrosoftFastStdCallMangling()) {
401 PlatformParser = createCOFFAsmParser();
402 PlatformParser->Initialize(*this);
403 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000404 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000405 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000406 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000407 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000408 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000409 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000410}
411
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000412AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000413 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
414
415 // Destroy any macros.
416 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
417 ie = MacroMap.end(); it != ie; ++it)
418 delete it->getValue();
419
Daniel Dunbare4749702010-07-12 18:12:02 +0000420 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000421 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000422}
423
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000424void AsmParser::PrintMacroInstantiations() {
425 // Print the active macro instantiation stack.
426 for (std::vector<MacroInstantiation*>::const_reverse_iterator
427 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000428 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
429 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000430}
431
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000432bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000433 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000434 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000435 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000436 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000437 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000438}
439
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000440bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000441 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000442 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000443 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000444 return true;
445}
446
Sean Callananfd0b0282010-01-21 00:19:58 +0000447bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000448 std::string IncludedFile;
449 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000450 if (NewBuf == -1)
451 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000452
Sean Callananfd0b0282010-01-21 00:19:58 +0000453 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000454
Sean Callananfd0b0282010-01-21 00:19:58 +0000455 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000456
Sean Callananfd0b0282010-01-21 00:19:58 +0000457 return false;
458}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000459
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000460/// Process the specified .incbin file by seaching for it in the include paths
461/// then just emiting the byte contents of the file to the streamer. This
462/// returns true on failure.
463bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
464 std::string IncludedFile;
465 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
466 if (NewBuf == -1)
467 return true;
468
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000469 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000470 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
471 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000472 return false;
473}
474
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000475void AsmParser::JumpToLoc(SMLoc Loc) {
476 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
477 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
478}
479
Sean Callananfd0b0282010-01-21 00:19:58 +0000480const AsmToken &AsmParser::Lex() {
481 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000482
Sean Callananfd0b0282010-01-21 00:19:58 +0000483 if (tok->is(AsmToken::Eof)) {
484 // If this is the end of an included file, pop the parent file off the
485 // include stack.
486 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
487 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000488 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000489 tok = &Lexer.Lex();
490 }
491 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000492
Sean Callananfd0b0282010-01-21 00:19:58 +0000493 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000494 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000495
Sean Callananfd0b0282010-01-21 00:19:58 +0000496 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000497}
498
Chris Lattner79180e22010-04-05 23:15:42 +0000499bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000500 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000501 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000502 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000503
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000504 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000505 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000506
507 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000508 AsmCond StartingCondState = TheCondState;
509
Kevin Enderby613b7572011-11-01 22:27:22 +0000510 // If we are generating dwarf for assembly source files save the initial text
511 // section and generate a .file directive.
512 if (getContext().getGenDwarfForAssembly()) {
513 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000514 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
515 getStreamer().EmitLabel(SectionStartSym);
516 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000517 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
518 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
519 }
520
Chris Lattnerb717fb02009-07-02 21:53:43 +0000521 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000522 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000523 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000524
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000525 // We had an error, validate that one was emitted and recover by skipping to
526 // the next line.
527 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000528 EatToEndOfStatement();
529 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000530
531 if (TheCondState.TheCond != StartingCondState.TheCond ||
532 TheCondState.Ignore != StartingCondState.Ignore)
533 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000534
535 // Check to see there are no empty DwarfFile slots.
536 const std::vector<MCDwarfFile *> &MCDwarfFiles =
537 getContext().getMCDwarfFiles();
538 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000539 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000540 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000541 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000542
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000543 // Check to see that all assembler local symbols were actually defined.
544 // Targets that don't do subsections via symbols may not want this, though,
545 // so conservatively exclude them. Only do this if we're finalizing, though,
546 // as otherwise we won't necessarilly have seen everything yet.
547 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
548 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
549 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
550 e = Symbols.end();
551 i != e; ++i) {
552 MCSymbol *Sym = i->getValue();
553 // Variable symbols may not be marked as defined, so check those
554 // explicitly. If we know it's a variable, we have a definition for
555 // the purposes of this check.
556 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
557 // FIXME: We would really like to refer back to where the symbol was
558 // first referenced for a source location. We need to add something
559 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000560 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
561 "assembler local symbol '" + Sym->getName() +
562 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000563 }
564 }
565
566
Chris Lattner79180e22010-04-05 23:15:42 +0000567 // Finalize the output stream if there are no errors and if the client wants
568 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000569 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000570 Out.Finish();
571
Chris Lattnerb717fb02009-07-02 21:53:43 +0000572 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000573}
574
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000575void AsmParser::CheckForValidSection() {
576 if (!getStreamer().getCurrentSection()) {
577 TokError("expected section directive before assembly directive");
578 Out.SwitchSection(Ctx.getMachOSection(
579 "__TEXT", "__text",
580 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
581 0, SectionKind::getText()));
582 }
583}
584
Chris Lattner2cf5f142009-06-22 01:29:09 +0000585/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
586void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000587 while (Lexer.isNot(AsmToken::EndOfStatement) &&
588 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000589 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000590
Chris Lattner2cf5f142009-06-22 01:29:09 +0000591 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000592 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000593 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000594}
595
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000596StringRef AsmParser::ParseStringToEndOfStatement() {
597 const char *Start = getTok().getLoc().getPointer();
598
599 while (Lexer.isNot(AsmToken::EndOfStatement) &&
600 Lexer.isNot(AsmToken::Eof))
601 Lex();
602
603 const char *End = getTok().getLoc().getPointer();
604 return StringRef(Start, End - Start);
605}
Chris Lattnerc4193832009-06-22 05:51:26 +0000606
Chris Lattner74ec1a32009-06-22 06:32:03 +0000607/// ParseParenExpr - Parse a paren expression and return it.
608/// NOTE: This assumes the leading '(' has already been consumed.
609///
610/// parenexpr ::= expr)
611///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000612bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000613 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000614 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000615 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000616 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000617 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000618 return false;
619}
Chris Lattnerc4193832009-06-22 05:51:26 +0000620
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000621/// ParseBracketExpr - Parse a bracket expression and return it.
622/// NOTE: This assumes the leading '[' has already been consumed.
623///
624/// bracketexpr ::= expr]
625///
626bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
627 if (ParseExpression(Res)) return true;
628 if (Lexer.isNot(AsmToken::RBrac))
629 return TokError("expected ']' in brackets expression");
630 EndLoc = Lexer.getLoc();
631 Lex();
632 return false;
633}
634
Chris Lattner74ec1a32009-06-22 06:32:03 +0000635/// ParsePrimaryExpr - Parse a primary expression and return it.
636/// primaryexpr ::= (parenexpr
637/// primaryexpr ::= symbol
638/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000639/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000640/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000641bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000642 switch (Lexer.getKind()) {
643 default:
644 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000645 // If we have an error assume that we've already handled it.
646 case AsmToken::Error:
647 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000648 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000649 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000650 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000651 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000652 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000653 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000654 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000655 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000656 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000657 EndLoc = Lexer.getLoc();
658
659 StringRef Identifier;
660 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000661 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000662
Daniel Dunbarfffff912009-10-16 01:34:54 +0000663 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000664 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000665 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000666
667 // Lookup the symbol variant if used.
668 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000669 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000670 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000671 if (Variant == MCSymbolRefExpr::VK_Invalid) {
672 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000673 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000674 }
675 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000676
Daniel Dunbarfffff912009-10-16 01:34:54 +0000677 // If this is an absolute variable reference, substitute it now to preserve
678 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000679 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000680 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000681 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000682
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000683 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000684 return false;
685 }
686
687 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000688 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000689 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000690 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000691 case AsmToken::Integer: {
692 SMLoc Loc = getTok().getLoc();
693 int64_t IntVal = getTok().getIntVal();
694 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000695 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000696 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000697 // Look for 'b' or 'f' following an Integer as a directional label
698 if (Lexer.getKind() == AsmToken::Identifier) {
699 StringRef IDVal = getTok().getString();
700 if (IDVal == "f" || IDVal == "b"){
701 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
702 IDVal == "f" ? 1 : 0);
703 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
704 getContext());
705 if(IDVal == "b" && Sym->isUndefined())
706 return Error(Loc, "invalid reference to undefined symbol");
707 EndLoc = Lexer.getLoc();
708 Lex(); // Eat identifier.
709 }
710 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000711 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000712 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000713 case AsmToken::Real: {
714 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000715 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000716 Res = MCConstantExpr::Create(IntVal, getContext());
717 Lex(); // Eat token.
718 return false;
719 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000720 case AsmToken::Dot: {
721 // This is a '.' reference, which references the current PC. Emit a
722 // temporary label to the streamer and refer to it.
723 MCSymbol *Sym = Ctx.CreateTempSymbol();
724 Out.EmitLabel(Sym);
725 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
726 EndLoc = Lexer.getLoc();
727 Lex(); // Eat identifier.
728 return false;
729 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000730 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000731 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000732 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000733 case AsmToken::LBrac:
734 if (!PlatformParser->HasBracketExpressions())
735 return TokError("brackets expression not supported on this target");
736 Lex(); // Eat the '['.
737 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000738 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000739 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000740 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000741 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000742 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000743 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000744 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000745 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000746 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000747 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000748 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000749 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000750 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000751 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000752 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000753 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000754 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000755 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000756 }
757}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000758
Chris Lattnerb4307b32010-01-15 19:28:38 +0000759bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000760 SMLoc EndLoc;
761 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000762}
763
Daniel Dunbarcceba832010-09-17 02:47:07 +0000764const MCExpr *
765AsmParser::ApplyModifierToExpr(const MCExpr *E,
766 MCSymbolRefExpr::VariantKind Variant) {
767 // Recurse over the given expression, rebuilding it to apply the given variant
768 // if there is exactly one symbol.
769 switch (E->getKind()) {
770 case MCExpr::Target:
771 case MCExpr::Constant:
772 return 0;
773
774 case MCExpr::SymbolRef: {
775 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
776
777 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
778 TokError("invalid variant on expression '" +
779 getTok().getIdentifier() + "' (already modified)");
780 return E;
781 }
782
783 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
784 }
785
786 case MCExpr::Unary: {
787 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
788 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
789 if (!Sub)
790 return 0;
791 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
792 }
793
794 case MCExpr::Binary: {
795 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
796 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
797 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
798
799 if (!LHS && !RHS)
800 return 0;
801
802 if (!LHS) LHS = BE->getLHS();
803 if (!RHS) RHS = BE->getRHS();
804
805 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
806 }
807 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000808
Craig Topper85814382012-02-07 05:05:23 +0000809 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000810}
811
Chris Lattner74ec1a32009-06-22 06:32:03 +0000812/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000813///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000814/// expr ::= expr &&,|| expr -> lowest.
815/// expr ::= expr |,^,&,! expr
816/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
817/// expr ::= expr <<,>> expr
818/// expr ::= expr +,- expr
819/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000820/// expr ::= primaryexpr
821///
Chris Lattner54482b42010-01-15 19:39:23 +0000822bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000823 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000824 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000825 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
826 return true;
827
Daniel Dunbarcceba832010-09-17 02:47:07 +0000828 // As a special case, we support 'a op b @ modifier' by rewriting the
829 // expression to include the modifier. This is inefficient, but in general we
830 // expect users to use 'a@modifier op b'.
831 if (Lexer.getKind() == AsmToken::At) {
832 Lex();
833
834 if (Lexer.isNot(AsmToken::Identifier))
835 return TokError("unexpected symbol modifier following '@'");
836
837 MCSymbolRefExpr::VariantKind Variant =
838 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
839 if (Variant == MCSymbolRefExpr::VK_Invalid)
840 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
841
842 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
843 if (!ModifiedRes) {
844 return TokError("invalid modifier '" + getTok().getIdentifier() +
845 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000846 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000847
Daniel Dunbarcceba832010-09-17 02:47:07 +0000848 Res = ModifiedRes;
849 Lex();
850 }
851
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000852 // Try to constant fold it up front, if possible.
853 int64_t Value;
854 if (Res->EvaluateAsAbsolute(Value))
855 Res = MCConstantExpr::Create(Value, getContext());
856
857 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000858}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000859
Chris Lattnerb4307b32010-01-15 19:28:38 +0000860bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000861 Res = 0;
862 return ParseParenExpr(Res, EndLoc) ||
863 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000864}
865
Daniel Dunbar475839e2009-06-29 20:37:27 +0000866bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000867 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000868
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000869 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000870 if (ParseExpression(Expr))
871 return true;
872
Daniel Dunbare00b0112009-10-16 01:57:52 +0000873 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000874 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000875
876 return false;
877}
878
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000879static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000880 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000881 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000882 default:
883 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000884
Jim Grosbachfbe16812011-08-20 16:24:13 +0000885 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000886 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000887 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000888 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000889 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000890 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000891 return 1;
892
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000893
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000894 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000895 //
896 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000897 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000898 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000899 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000900 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000901 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000902 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000903 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000904 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000905 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000906
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000907 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000908 case AsmToken::EqualEqual:
909 Kind = MCBinaryExpr::EQ;
910 return 3;
911 case AsmToken::ExclaimEqual:
912 case AsmToken::LessGreater:
913 Kind = MCBinaryExpr::NE;
914 return 3;
915 case AsmToken::Less:
916 Kind = MCBinaryExpr::LT;
917 return 3;
918 case AsmToken::LessEqual:
919 Kind = MCBinaryExpr::LTE;
920 return 3;
921 case AsmToken::Greater:
922 Kind = MCBinaryExpr::GT;
923 return 3;
924 case AsmToken::GreaterEqual:
925 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000926 return 3;
927
Jim Grosbachfbe16812011-08-20 16:24:13 +0000928 // Intermediate Precedence: <<, >>
929 case AsmToken::LessLess:
930 Kind = MCBinaryExpr::Shl;
931 return 4;
932 case AsmToken::GreaterGreater:
933 Kind = MCBinaryExpr::Shr;
934 return 4;
935
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000936 // High Intermediate Precedence: +, -
937 case AsmToken::Plus:
938 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000939 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000940 case AsmToken::Minus:
941 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000942 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000943
Jim Grosbachfbe16812011-08-20 16:24:13 +0000944 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000945 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000946 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000947 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000948 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000949 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000950 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000951 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000952 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000953 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000954 }
955}
956
957
958/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
959/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000960bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
961 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000962 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000963 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000964 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000965
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000966 // If the next token is lower precedence than we are allowed to eat, return
967 // successfully with what we ate already.
968 if (TokPrec < Precedence)
969 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000970
Sean Callanan79ed1a82010-01-19 20:22:31 +0000971 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000972
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000973 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000974 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000975 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000976
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000977 // If BinOp binds less tightly with RHS than the operator after RHS, let
978 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000979 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000980 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000981 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000982 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000983 }
984
Daniel Dunbar475839e2009-06-29 20:37:27 +0000985 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000986 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000987 }
988}
989
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000990
991
992
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000993/// ParseStatement:
994/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000995/// ::= Label* Directive ...Operands... EndOfStatement
996/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000997bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000998 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000999 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001000 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001001 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001002 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001003
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001004 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001005 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001006 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001007 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001008 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001009 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001010 if (Lexer.is(AsmToken::Hash))
1011 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001012
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001013 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001014 if (Lexer.is(AsmToken::Integer)) {
1015 LocalLabelVal = getTok().getIntVal();
1016 if (LocalLabelVal < 0) {
1017 if (!TheCondState.Ignore)
1018 return TokError("unexpected token at start of statement");
1019 IDVal = "";
1020 }
1021 else {
1022 IDVal = getTok().getString();
1023 Lex(); // Consume the integer token to be used as an identifier token.
1024 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001025 if (!TheCondState.Ignore)
1026 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001027 }
1028 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001029
1030 } else if (Lexer.is(AsmToken::Dot)) {
1031 // Treat '.' as a valid identifier in this context.
1032 Lex();
1033 IDVal = ".";
1034
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001035 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001036 if (!TheCondState.Ignore)
1037 return TokError("unexpected token at start of statement");
1038 IDVal = "";
1039 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001040
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001041
Chris Lattner7834fac2010-04-17 18:14:27 +00001042 // Handle conditional assembly here before checking for skipping. We
1043 // have to do this so that .endif isn't skipped in a ".if 0" block for
1044 // example.
1045 if (IDVal == ".if")
1046 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001047 if (IDVal == ".ifb")
1048 return ParseDirectiveIfb(IDLoc, true);
1049 if (IDVal == ".ifnb")
1050 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001051 if (IDVal == ".ifdef")
1052 return ParseDirectiveIfdef(IDLoc, true);
1053 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1054 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001055 if (IDVal == ".elseif")
1056 return ParseDirectiveElseIf(IDLoc);
1057 if (IDVal == ".else")
1058 return ParseDirectiveElse(IDLoc);
1059 if (IDVal == ".endif")
1060 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001061
Chris Lattner7834fac2010-04-17 18:14:27 +00001062 // If we are in a ".if 0" block, ignore this statement.
1063 if (TheCondState.Ignore) {
1064 EatToEndOfStatement();
1065 return false;
1066 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001067
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001068 // FIXME: Recurse on local labels?
1069
1070 // See what kind of statement we have.
1071 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001072 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001073 CheckForValidSection();
1074
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001075 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001076 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001077
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001078 // Diagnose attempt to use '.' as a label.
1079 if (IDVal == ".")
1080 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1081
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001082 // Diagnose attempt to use a variable as a label.
1083 //
1084 // FIXME: Diagnostics. Note the location of the definition as a label.
1085 // FIXME: This doesn't diagnose assignment to a symbol which has been
1086 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001087 MCSymbol *Sym;
1088 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001089 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001090 else
1091 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001092 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001093 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001094
Daniel Dunbar959fd882009-08-26 22:13:22 +00001095 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001096 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001097
Kevin Enderby94c2e852011-12-09 18:09:40 +00001098 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001099 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001100 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001101 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1102 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001103
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001104 // Consume any end of statement token, if present, to avoid spurious
1105 // AddBlankLine calls().
1106 if (Lexer.is(AsmToken::EndOfStatement)) {
1107 Lex();
1108 if (Lexer.is(AsmToken::Eof))
1109 return false;
1110 }
1111
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001112 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001113 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001114
Daniel Dunbar3f872332009-07-28 16:08:33 +00001115 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001116 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001117 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001118
Nico Weber4c4c7322011-01-28 03:04:41 +00001119 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001120
1121 default: // Normal instruction or directive.
1122 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001123 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001124
1125 // If macros are enabled, check to see if this is a macro instantiation.
1126 if (MacrosEnabled)
1127 if (const Macro *M = MacroMap.lookup(IDVal))
1128 return HandleMacroEntry(IDVal, IDLoc, M);
1129
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001130 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001131 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001132 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001133 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001134 return ParseDirectiveSet(IDVal, true);
1135 if (IDVal == ".equiv")
1136 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001137
Daniel Dunbara0d14262009-06-24 23:30:00 +00001138 // Data directives
1139
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001140 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001141 return ParseDirectiveAscii(IDVal, false);
1142 if (IDVal == ".asciz" || IDVal == ".string")
1143 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001144
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001145 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001146 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001147 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001148 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001149 if (IDVal == ".value")
1150 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001151 if (IDVal == ".2byte")
1152 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001153 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001154 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001155 if (IDVal == ".int")
1156 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001157 if (IDVal == ".4byte")
1158 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001159 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001160 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001161 if (IDVal == ".8byte")
1162 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001163 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001164 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1165 if (IDVal == ".double")
1166 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001167
Eli Friedman5d68ec22010-07-19 04:17:25 +00001168 if (IDVal == ".align") {
1169 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1170 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1171 }
1172 if (IDVal == ".align32") {
1173 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1174 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1175 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001176 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001177 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001178 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001179 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001180 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001181 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001182 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001183 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001184 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001185 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001186 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001187 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1188
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001189 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001190 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001191
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001192 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001193 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001194 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001195 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001196 if (IDVal == ".zero")
1197 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001198
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001199 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001200
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001201 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001202 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001203 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001204 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001205 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001206 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001207 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001208 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001209 if (IDVal == ".symbol_resolver")
1210 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001211 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001212 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001213 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001214 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001215 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001216 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001217 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001218 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001219 if (IDVal == ".weak_def_can_be_hidden")
1220 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001221
Hans Wennborg5cc64912011-06-18 13:51:54 +00001222 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001223 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001224 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001225 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001226
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001227 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001228 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001229 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001230 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001231 if (IDVal == ".incbin")
1232 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001233
Evan Chengbd27f5a2011-07-27 00:38:12 +00001234 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001235 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001236
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001237 // Look up the handler in the handler table.
1238 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1239 DirectiveMap.lookup(IDVal);
1240 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001241 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001242
Kevin Enderby9c656452009-09-10 20:51:44 +00001243 // Target hook for parsing target specific directives.
1244 if (!getTargetParser().ParseDirective(ID))
1245 return false;
1246
Jim Grosbach686c0182012-05-01 18:38:27 +00001247 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001248 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001249
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001250 CheckForValidSection();
1251
Chris Lattnera7f13542010-05-19 23:34:33 +00001252 // Canonicalize the opcode to lower case.
1253 SmallString<128> Opcode;
1254 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1255 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001256
Chris Lattner98986712010-01-14 22:21:20 +00001257 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001258 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001259 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001260
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001261 // Dump the parsed representation, if requested.
1262 if (getShowParsedOperands()) {
1263 SmallString<256> Str;
1264 raw_svector_ostream OS(Str);
1265 OS << "parsed instruction: [";
1266 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1267 if (i != 0)
1268 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001269 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001270 }
1271 OS << "]";
1272
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001273 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001274 }
1275
Kevin Enderby613b7572011-11-01 22:27:22 +00001276 // If we are generating dwarf for assembly source files and the current
1277 // section is the initial text section then generate a .loc directive for
1278 // the instruction.
1279 if (!HadError && getContext().getGenDwarfForAssembly() &&
1280 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1281 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1282 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1283 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001284 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001285 StringRef());
1286 }
1287
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001288 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001289 if (!HadError)
1290 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1291 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001292
Chris Lattner98986712010-01-14 22:21:20 +00001293 // Free any parsed operands.
1294 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1295 delete ParsedOperands[i];
1296
Chris Lattnercbf8a982010-09-11 16:18:25 +00001297 // Don't skip the rest of the line, the instruction parser is responsible for
1298 // that.
1299 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001300}
Chris Lattner9a023f72009-06-24 04:43:34 +00001301
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001302/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1303/// since they may not be able to be tokenized to get to the end of line token.
1304void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001305 if (!Lexer.is(AsmToken::EndOfStatement))
1306 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001307 // Eat EOL.
1308 Lex();
1309}
1310
1311/// ParseCppHashLineFilenameComment as this:
1312/// ::= # number "filename"
1313/// or just as a full line comment if it doesn't have a number and a string.
1314bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1315 Lex(); // Eat the hash token.
1316
1317 if (getLexer().isNot(AsmToken::Integer)) {
1318 // Consume the line since in cases it is not a well-formed line directive,
1319 // as if were simply a full line comment.
1320 EatToEndOfLine();
1321 return false;
1322 }
1323
1324 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001325 Lex();
1326
1327 if (getLexer().isNot(AsmToken::String)) {
1328 EatToEndOfLine();
1329 return false;
1330 }
1331
1332 StringRef Filename = getTok().getString();
1333 // Get rid of the enclosing quotes.
1334 Filename = Filename.substr(1, Filename.size()-2);
1335
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001336 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1337 CppHashLoc = L;
1338 CppHashFilename = Filename;
1339 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001340
1341 // Ignore any trailing characters, they're just comment.
1342 EatToEndOfLine();
1343 return false;
1344}
1345
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001346/// DiagHandler - will use the the last parsed cpp hash line filename comment
1347/// for the Filename and LineNo if any in the diagnostic.
1348void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1349 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1350 raw_ostream &OS = errs();
1351
1352 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1353 const SMLoc &DiagLoc = Diag.getLoc();
1354 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1355 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1356
1357 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1358 // before printing the message.
1359 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001360 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001361 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1362 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1363 }
1364
1365 // If we have not parsed a cpp hash line filename comment or the source
1366 // manager changed or buffer changed (like in a nested include) then just
1367 // print the normal diagnostic using its Filename and LineNo.
1368 if (!Parser->CppHashLineNumber ||
1369 &DiagSrcMgr != &Parser->SrcMgr ||
1370 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001371 if (Parser->SavedDiagHandler)
1372 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1373 else
1374 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001375 return;
1376 }
1377
1378 // Use the CppHashFilename and calculate a line number based on the
1379 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1380 // the diagnostic.
1381 const std::string Filename = Parser->CppHashFilename;
1382
1383 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1384 int CppHashLocLineNo =
1385 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1386 int LineNo = Parser->CppHashLineNumber - 1 +
1387 (DiagLocLineNo - CppHashLocLineNo);
1388
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001389 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1390 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001391 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001392 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001393
Benjamin Kramer04a04262011-10-16 10:48:29 +00001394 if (Parser->SavedDiagHandler)
1395 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1396 else
1397 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001398}
1399
Rafael Espindola65366442011-06-05 02:43:45 +00001400bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1401 const std::vector<StringRef> &Parameters,
1402 const std::vector<std::vector<AsmToken> > &A,
1403 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001404 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001405 unsigned NParameters = Parameters.size();
1406 if (NParameters != 0 && NParameters != A.size())
1407 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001408
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001409 while (!Body.empty()) {
1410 // Scan for the next substitution.
1411 std::size_t End = Body.size(), Pos = 0;
1412 for (; Pos != End; ++Pos) {
1413 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001414 if (!NParameters) {
1415 // This macro has no parameters, look for $0, $1, etc.
1416 if (Body[Pos] != '$' || Pos + 1 == End)
1417 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001418
Rafael Espindola65366442011-06-05 02:43:45 +00001419 char Next = Body[Pos + 1];
1420 if (Next == '$' || Next == 'n' || isdigit(Next))
1421 break;
1422 } else {
1423 // This macro has parameters, look for \foo, \bar, etc.
1424 if (Body[Pos] == '\\' && Pos + 1 != End)
1425 break;
1426 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001427 }
1428
1429 // Add the prefix.
1430 OS << Body.slice(0, Pos);
1431
1432 // Check if we reached the end.
1433 if (Pos == End)
1434 break;
1435
Rafael Espindola65366442011-06-05 02:43:45 +00001436 if (!NParameters) {
1437 switch (Body[Pos+1]) {
1438 // $$ => $
1439 case '$':
1440 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001441 break;
1442
Rafael Espindola65366442011-06-05 02:43:45 +00001443 // $n => number of arguments
1444 case 'n':
1445 OS << A.size();
1446 break;
1447
1448 // $[0-9] => argument
1449 default: {
1450 // Missing arguments are ignored.
1451 unsigned Index = Body[Pos+1] - '0';
1452 if (Index >= A.size())
1453 break;
1454
1455 // Otherwise substitute with the token values, with spaces eliminated.
1456 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1457 ie = A[Index].end(); it != ie; ++it)
1458 OS << it->getString();
1459 break;
1460 }
1461 }
1462 Pos += 2;
1463 } else {
1464 unsigned I = Pos + 1;
1465 while (isalnum(Body[I]) && I + 1 != End)
1466 ++I;
1467
1468 const char *Begin = Body.data() + Pos +1;
1469 StringRef Argument(Begin, I - (Pos +1));
1470 unsigned Index = 0;
1471 for (; Index < NParameters; ++Index)
1472 if (Parameters[Index] == Argument)
1473 break;
1474
1475 // FIXME: We should error at the macro definition.
1476 if (Index == NParameters)
1477 return Error(L, "Parameter not found");
1478
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001479 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1480 ie = A[Index].end(); it != ie; ++it)
1481 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001482
Rafael Espindola65366442011-06-05 02:43:45 +00001483 Pos += 1 + Argument.size();
1484 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001485 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001486 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001487 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001488
1489 // We include the .endmacro in the buffer as our queue to exit the macro
1490 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001491 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001492 return false;
1493}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001494
Rafael Espindola65366442011-06-05 02:43:45 +00001495MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1496 MemoryBuffer *I)
1497 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1498{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001499}
1500
1501bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1502 const Macro *M) {
1503 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1504 // this, although we should protect against infinite loops.
1505 if (ActiveMacros.size() == 20)
1506 return TokError("macros cannot be nested more than 20 levels deep");
1507
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001508 // Parse the macro instantiation arguments.
1509 std::vector<std::vector<AsmToken> > MacroArguments;
1510 MacroArguments.push_back(std::vector<AsmToken>());
1511 unsigned ParenLevel = 0;
1512 for (;;) {
1513 if (Lexer.is(AsmToken::Eof))
1514 return TokError("unexpected token in macro instantiation");
1515 if (Lexer.is(AsmToken::EndOfStatement))
1516 break;
1517
1518 // If we aren't inside parentheses and this is a comma, start a new token
1519 // list.
1520 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1521 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001522 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001523 // Adjust the current parentheses level.
1524 if (Lexer.is(AsmToken::LParen))
1525 ++ParenLevel;
1526 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1527 --ParenLevel;
1528
1529 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001530 MacroArguments.back().push_back(getTok());
1531 }
1532 Lex();
1533 }
Jim Grosbach68f89a62012-04-16 21:18:49 +00001534 // If the last argument didn't end up with any tokens, it's not a real
1535 // argument and we should remove it from the list. This happens with either
1536 // a tailing comma or an empty argument list.
1537 if (MacroArguments.back().empty())
1538 MacroArguments.pop_back();
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001539
Rafael Espindola65366442011-06-05 02:43:45 +00001540 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1541 // to hold the macro body with substitutions.
1542 SmallString<256> Buf;
1543 StringRef Body = M->Body;
1544
1545 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1546 return true;
1547
1548 MemoryBuffer *Instantiation =
1549 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1550
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001551 // Create the macro instantiation object and add to the current macro
1552 // instantiation stack.
1553 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001554 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001555 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001556 ActiveMacros.push_back(MI);
1557
1558 // Jump to the macro instantiation and prime the lexer.
1559 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1560 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1561 Lex();
1562
1563 return false;
1564}
1565
1566void AsmParser::HandleMacroExit() {
1567 // Jump to the EndOfStatement we should return to, and consume it.
1568 JumpToLoc(ActiveMacros.back()->ExitLoc);
1569 Lex();
1570
1571 // Pop the instantiation entry.
1572 delete ActiveMacros.back();
1573 ActiveMacros.pop_back();
1574}
1575
Rafael Espindolae71cc862012-01-28 05:57:00 +00001576static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001577 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001578 case MCExpr::Binary: {
1579 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1580 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001581 break;
1582 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001583 case MCExpr::Target:
1584 case MCExpr::Constant:
1585 return false;
1586 case MCExpr::SymbolRef: {
1587 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001588 if (S.isVariable())
1589 return IsUsedIn(Sym, S.getVariableValue());
1590 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001591 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001592 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001593 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001594 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001595
1596 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001597}
1598
Nico Weber4c4c7322011-01-28 03:04:41 +00001599bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001600 // FIXME: Use better location, we should use proper tokens.
1601 SMLoc EqualLoc = Lexer.getLoc();
1602
Daniel Dunbar821e3332009-08-31 08:09:28 +00001603 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001604 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001605 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001606
Rafael Espindolae71cc862012-01-28 05:57:00 +00001607 // Note: we don't count b as used in "a = b". This is to allow
1608 // a = b
1609 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001610
Daniel Dunbar3f872332009-07-28 16:08:33 +00001611 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001612 return TokError("unexpected token in assignment");
1613
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001614 // Error on assignment to '.'.
1615 if (Name == ".") {
1616 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1617 "(use '.space' or '.org').)"));
1618 }
1619
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001620 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001621 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001622
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001623 // Validate that the LHS is allowed to be a variable (either it has not been
1624 // used as a symbol, or it is an absolute symbol).
1625 MCSymbol *Sym = getContext().LookupSymbol(Name);
1626 if (Sym) {
1627 // Diagnose assignment to a label.
1628 //
1629 // FIXME: Diagnostics. Note the location of the definition as a label.
1630 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001631 if (IsUsedIn(Sym, Value))
1632 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1633 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001634 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001635 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1636 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001637 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001638 return Error(EqualLoc, "redefinition of '" + Name + "'");
1639 else if (!Sym->isVariable())
1640 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001641 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001642 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1643 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001644
1645 // Don't count these checks as uses.
1646 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001647 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001648 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001649
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001650 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001651
1652 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001653 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001654
1655 return false;
1656}
1657
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001658/// ParseIdentifier:
1659/// ::= identifier
1660/// ::= string
1661bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001662 // The assembler has relaxed rules for accepting identifiers, in particular we
1663 // allow things like '.globl $foo', which would normally be separate
1664 // tokens. At this level, we have already lexed so we cannot (currently)
1665 // handle this as a context dependent token, instead we detect adjacent tokens
1666 // and return the combined identifier.
1667 if (Lexer.is(AsmToken::Dollar)) {
1668 SMLoc DollarLoc = getLexer().getLoc();
1669
1670 // Consume the dollar sign, and check for a following identifier.
1671 Lex();
1672 if (Lexer.isNot(AsmToken::Identifier))
1673 return true;
1674
1675 // We have a '$' followed by an identifier, make sure they are adjacent.
1676 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1677 return true;
1678
1679 // Construct the joined identifier and consume the token.
1680 Res = StringRef(DollarLoc.getPointer(),
1681 getTok().getIdentifier().size() + 1);
1682 Lex();
1683 return false;
1684 }
1685
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001686 if (Lexer.isNot(AsmToken::Identifier) &&
1687 Lexer.isNot(AsmToken::String))
1688 return true;
1689
Sean Callanan18b83232010-01-19 21:44:56 +00001690 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001691
Sean Callanan79ed1a82010-01-19 20:22:31 +00001692 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001693
1694 return false;
1695}
1696
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001697/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001698/// ::= .equ identifier ',' expression
1699/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001700/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001701bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001702 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001703
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001704 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001705 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001706
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001707 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001708 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001709 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001710
Nico Weber4c4c7322011-01-28 03:04:41 +00001711 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001712}
1713
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001714bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001715 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001716
1717 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001718 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001719 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1720 if (Str[i] != '\\') {
1721 Data += Str[i];
1722 continue;
1723 }
1724
1725 // Recognize escaped characters. Note that this escape semantics currently
1726 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1727 ++i;
1728 if (i == e)
1729 return TokError("unexpected backslash at end of string");
1730
1731 // Recognize octal sequences.
1732 if ((unsigned) (Str[i] - '0') <= 7) {
1733 // Consume up to three octal characters.
1734 unsigned Value = Str[i] - '0';
1735
1736 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1737 ++i;
1738 Value = Value * 8 + (Str[i] - '0');
1739
1740 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1741 ++i;
1742 Value = Value * 8 + (Str[i] - '0');
1743 }
1744 }
1745
1746 if (Value > 255)
1747 return TokError("invalid octal escape sequence (out of range)");
1748
1749 Data += (unsigned char) Value;
1750 continue;
1751 }
1752
1753 // Otherwise recognize individual escapes.
1754 switch (Str[i]) {
1755 default:
1756 // Just reject invalid escape sequences for now.
1757 return TokError("invalid escape sequence (unrecognized character)");
1758
1759 case 'b': Data += '\b'; break;
1760 case 'f': Data += '\f'; break;
1761 case 'n': Data += '\n'; break;
1762 case 'r': Data += '\r'; break;
1763 case 't': Data += '\t'; break;
1764 case '"': Data += '"'; break;
1765 case '\\': Data += '\\'; break;
1766 }
1767 }
1768
1769 return false;
1770}
1771
Daniel Dunbara0d14262009-06-24 23:30:00 +00001772/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001773/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1774bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001775 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001776 CheckForValidSection();
1777
Daniel Dunbara0d14262009-06-24 23:30:00 +00001778 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001779 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001780 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001781
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001782 std::string Data;
1783 if (ParseEscapedString(Data))
1784 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001785
1786 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001787 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001788 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1789
Sean Callanan79ed1a82010-01-19 20:22:31 +00001790 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001791
1792 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001793 break;
1794
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001795 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001796 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001797 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001798 }
1799 }
1800
Sean Callanan79ed1a82010-01-19 20:22:31 +00001801 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001802 return false;
1803}
1804
1805/// ParseDirectiveValue
1806/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1807bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001808 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001809 CheckForValidSection();
1810
Daniel Dunbara0d14262009-06-24 23:30:00 +00001811 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001812 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001813 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001814 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001815 return true;
1816
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001817 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001818 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1819 assert(Size <= 8 && "Invalid size");
1820 uint64_t IntValue = MCE->getValue();
1821 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1822 return Error(ExprLoc, "literal value out of range for directive");
1823 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1824 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001825 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001826
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001827 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001828 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001829
Daniel Dunbara0d14262009-06-24 23:30:00 +00001830 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001831 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001832 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001833 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001834 }
1835 }
1836
Sean Callanan79ed1a82010-01-19 20:22:31 +00001837 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001838 return false;
1839}
1840
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001841/// ParseDirectiveRealValue
1842/// ::= (.single | .double) [ expression (, expression)* ]
1843bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1844 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1845 CheckForValidSection();
1846
1847 for (;;) {
1848 // We don't truly support arithmetic on floating point expressions, so we
1849 // have to manually parse unary prefixes.
1850 bool IsNeg = false;
1851 if (getLexer().is(AsmToken::Minus)) {
1852 Lex();
1853 IsNeg = true;
1854 } else if (getLexer().is(AsmToken::Plus))
1855 Lex();
1856
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001857 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001858 getLexer().isNot(AsmToken::Real) &&
1859 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001860 return TokError("unexpected token in directive");
1861
1862 // Convert to an APFloat.
1863 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001864 StringRef IDVal = getTok().getString();
1865 if (getLexer().is(AsmToken::Identifier)) {
1866 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1867 Value = APFloat::getInf(Semantics);
1868 else if (!IDVal.compare_lower("nan"))
1869 Value = APFloat::getNaN(Semantics, false, ~0);
1870 else
1871 return TokError("invalid floating point literal");
1872 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001873 APFloat::opInvalidOp)
1874 return TokError("invalid floating point literal");
1875 if (IsNeg)
1876 Value.changeSign();
1877
1878 // Consume the numeric token.
1879 Lex();
1880
1881 // Emit the value as an integer.
1882 APInt AsInt = Value.bitcastToAPInt();
1883 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1884 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1885
1886 if (getLexer().is(AsmToken::EndOfStatement))
1887 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001888
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001889 if (getLexer().isNot(AsmToken::Comma))
1890 return TokError("unexpected token in directive");
1891 Lex();
1892 }
1893 }
1894
1895 Lex();
1896 return false;
1897}
1898
Daniel Dunbara0d14262009-06-24 23:30:00 +00001899/// ParseDirectiveSpace
1900/// ::= .space expression [ , expression ]
1901bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001902 CheckForValidSection();
1903
Daniel Dunbara0d14262009-06-24 23:30:00 +00001904 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001905 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001906 return true;
1907
1908 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001909 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1910 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001911 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001912 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001913
Daniel Dunbar475839e2009-06-29 20:37:27 +00001914 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001915 return true;
1916
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001917 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001918 return TokError("unexpected token in '.space' directive");
1919 }
1920
Sean Callanan79ed1a82010-01-19 20:22:31 +00001921 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001922
1923 if (NumBytes <= 0)
1924 return TokError("invalid number of bytes in '.space' directive");
1925
1926 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001927 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001928
1929 return false;
1930}
1931
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001932/// ParseDirectiveZero
1933/// ::= .zero expression
1934bool AsmParser::ParseDirectiveZero() {
1935 CheckForValidSection();
1936
1937 int64_t NumBytes;
1938 if (ParseAbsoluteExpression(NumBytes))
1939 return true;
1940
Rafael Espindolae452b172010-10-05 19:42:57 +00001941 int64_t Val = 0;
1942 if (getLexer().is(AsmToken::Comma)) {
1943 Lex();
1944 if (ParseAbsoluteExpression(Val))
1945 return true;
1946 }
1947
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001948 if (getLexer().isNot(AsmToken::EndOfStatement))
1949 return TokError("unexpected token in '.zero' directive");
1950
1951 Lex();
1952
Rafael Espindolae452b172010-10-05 19:42:57 +00001953 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001954
1955 return false;
1956}
1957
Daniel Dunbara0d14262009-06-24 23:30:00 +00001958/// ParseDirectiveFill
1959/// ::= .fill expression , expression , expression
1960bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001961 CheckForValidSection();
1962
Daniel Dunbara0d14262009-06-24 23:30:00 +00001963 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001964 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001965 return true;
1966
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001967 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001968 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001969 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001970
Daniel Dunbara0d14262009-06-24 23:30:00 +00001971 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001972 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001973 return true;
1974
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001975 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001976 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001977 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001978
Daniel Dunbara0d14262009-06-24 23:30:00 +00001979 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001980 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001981 return true;
1982
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001983 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001984 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001985
Sean Callanan79ed1a82010-01-19 20:22:31 +00001986 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001987
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001988 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1989 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001990
1991 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001992 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001993
1994 return false;
1995}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001996
1997/// ParseDirectiveOrg
1998/// ::= .org expression [ , expression ]
1999bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002000 CheckForValidSection();
2001
Daniel Dunbar821e3332009-08-31 08:09:28 +00002002 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002003 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002004 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002005 return true;
2006
2007 // Parse optional fill expression.
2008 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002009 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2010 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002011 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002012 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002013
Daniel Dunbar475839e2009-06-29 20:37:27 +00002014 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002015 return true;
2016
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002017 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002018 return TokError("unexpected token in '.org' directive");
2019 }
2020
Sean Callanan79ed1a82010-01-19 20:22:31 +00002021 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002022
Jim Grosbachebd4c052012-01-27 00:37:08 +00002023 // Only limited forms of relocatable expressions are accepted here, it
2024 // has to be relative to the current section. The streamer will return
2025 // 'true' if the expression wasn't evaluatable.
2026 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2027 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002028
2029 return false;
2030}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002031
2032/// ParseDirectiveAlign
2033/// ::= {.align, ...} expression [ , expression [ , expression ]]
2034bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002035 CheckForValidSection();
2036
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002037 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002038 int64_t Alignment;
2039 if (ParseAbsoluteExpression(Alignment))
2040 return true;
2041
2042 SMLoc MaxBytesLoc;
2043 bool HasFillExpr = false;
2044 int64_t FillExpr = 0;
2045 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002046 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2047 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002048 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002049 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002050
2051 // The fill expression can be omitted while specifying a maximum number of
2052 // alignment bytes, e.g:
2053 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002054 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002055 HasFillExpr = true;
2056 if (ParseAbsoluteExpression(FillExpr))
2057 return true;
2058 }
2059
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002060 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2061 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002062 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002063 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002064
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002065 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002066 if (ParseAbsoluteExpression(MaxBytesToFill))
2067 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002068
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002069 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002070 return TokError("unexpected token in directive");
2071 }
2072 }
2073
Sean Callanan79ed1a82010-01-19 20:22:31 +00002074 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002075
Daniel Dunbar648ac512010-05-17 21:54:30 +00002076 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002077 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002078
2079 // Compute alignment in bytes.
2080 if (IsPow2) {
2081 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002082 if (Alignment >= 32) {
2083 Error(AlignmentLoc, "invalid alignment value");
2084 Alignment = 31;
2085 }
2086
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002087 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002088 }
2089
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002090 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002091 if (MaxBytesLoc.isValid()) {
2092 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002093 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2094 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002095 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002096 }
2097
2098 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002099 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2100 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002101 MaxBytesToFill = 0;
2102 }
2103 }
2104
Daniel Dunbar648ac512010-05-17 21:54:30 +00002105 // Check whether we should use optimal code alignment for this .align
2106 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002107 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002108 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2109 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002110 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002111 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002112 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002113 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2114 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002115 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002116
2117 return false;
2118}
2119
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002120/// ParseDirectiveSymbolAttribute
2121/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002122bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002123 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002124 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002125 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002126 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002127
2128 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002129 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002130
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002131 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002132
Jim Grosbach10ec6502011-09-15 17:56:49 +00002133 // Assembler local symbols don't make any sense here. Complain loudly.
2134 if (Sym->isTemporary())
2135 return Error(Loc, "non-local symbol required in directive");
2136
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002137 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002138
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002139 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002140 break;
2141
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002142 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002143 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002144 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002145 }
2146 }
2147
Sean Callanan79ed1a82010-01-19 20:22:31 +00002148 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002149 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002150}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002151
2152/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002153/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2154bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002155 CheckForValidSection();
2156
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002157 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002158 StringRef Name;
2159 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002160 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002161
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002162 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002163 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002164
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002165 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002166 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002167 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002168
2169 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002170 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002171 if (ParseAbsoluteExpression(Size))
2172 return true;
2173
2174 int64_t Pow2Alignment = 0;
2175 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002176 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002177 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002178 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002179 if (ParseAbsoluteExpression(Pow2Alignment))
2180 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002181
Chris Lattner258281d2010-01-19 06:22:22 +00002182 // If this target takes alignments in bytes (not log) validate and convert.
2183 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2184 if (!isPowerOf2_64(Pow2Alignment))
2185 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2186 Pow2Alignment = Log2_64(Pow2Alignment);
2187 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002188 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002189
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002190 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002191 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002192
Sean Callanan79ed1a82010-01-19 20:22:31 +00002193 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002194
Chris Lattner1fc3d752009-07-09 17:25:12 +00002195 // NOTE: a size of zero for a .comm should create a undefined symbol
2196 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002197 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002198 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2199 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002200
Eric Christopherc260a3e2010-05-14 01:38:54 +00002201 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002202 // may internally end up wanting an alignment in bytes.
2203 // FIXME: Diagnose overflow.
2204 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002205 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2206 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002207
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002208 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002209 return Error(IDLoc, "invalid symbol redefinition");
2210
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002211 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002212 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002213 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002214 getStreamer().EmitZerofill(Ctx.getMachOSection(
2215 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2216 0, SectionKind::getBSS()),
2217 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002218 return false;
2219 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002220
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002221 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002222 return false;
2223}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002224
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002225/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002226/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002227bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002228 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002229 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002230
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002231 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002232 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002233 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002234
Sean Callanan79ed1a82010-01-19 20:22:31 +00002235 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002236
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002237 if (Str.empty())
2238 Error(Loc, ".abort detected. Assembly stopping.");
2239 else
2240 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002241 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002242
2243 return false;
2244}
Kevin Enderby71148242009-07-14 21:35:03 +00002245
Kevin Enderby1f049b22009-07-14 23:21:55 +00002246/// ParseDirectiveInclude
2247/// ::= .include "filename"
2248bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002249 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002250 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002251
Sean Callanan18b83232010-01-19 21:44:56 +00002252 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002253 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002254 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002255
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002256 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002257 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002258
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002259 // Strip the quotes.
2260 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002261
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002262 // Attempt to switch the lexer to the included file before consuming the end
2263 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002264 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002265 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002266 return true;
2267 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002268
2269 return false;
2270}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002271
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002272/// ParseDirectiveIncbin
2273/// ::= .incbin "filename"
2274bool AsmParser::ParseDirectiveIncbin() {
2275 if (getLexer().isNot(AsmToken::String))
2276 return TokError("expected string in '.incbin' directive");
2277
2278 std::string Filename = getTok().getString();
2279 SMLoc IncbinLoc = getLexer().getLoc();
2280 Lex();
2281
2282 if (getLexer().isNot(AsmToken::EndOfStatement))
2283 return TokError("unexpected token in '.incbin' directive");
2284
2285 // Strip the quotes.
2286 Filename = Filename.substr(1, Filename.size()-2);
2287
2288 // Attempt to process the included file.
2289 if (ProcessIncbinFile(Filename)) {
2290 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2291 return true;
2292 }
2293
2294 return false;
2295}
2296
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002297/// ParseDirectiveIf
2298/// ::= .if expression
2299bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002300 TheCondStack.push_back(TheCondState);
2301 TheCondState.TheCond = AsmCond::IfCond;
2302 if(TheCondState.Ignore) {
2303 EatToEndOfStatement();
2304 }
2305 else {
2306 int64_t ExprValue;
2307 if (ParseAbsoluteExpression(ExprValue))
2308 return true;
2309
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002310 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002311 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002312
Sean Callanan79ed1a82010-01-19 20:22:31 +00002313 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002314
2315 TheCondState.CondMet = ExprValue;
2316 TheCondState.Ignore = !TheCondState.CondMet;
2317 }
2318
2319 return false;
2320}
2321
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002322/// ParseDirectiveIfb
2323/// ::= .ifb string
2324bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2325 TheCondStack.push_back(TheCondState);
2326 TheCondState.TheCond = AsmCond::IfCond;
2327
2328 if(TheCondState.Ignore) {
2329 EatToEndOfStatement();
2330 } else {
2331 StringRef Str = ParseStringToEndOfStatement();
2332
2333 if (getLexer().isNot(AsmToken::EndOfStatement))
2334 return TokError("unexpected token in '.ifb' directive");
2335
2336 Lex();
2337
2338 TheCondState.CondMet = ExpectBlank == Str.empty();
2339 TheCondState.Ignore = !TheCondState.CondMet;
2340 }
2341
2342 return false;
2343}
2344
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002345bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2346 StringRef Name;
2347 TheCondStack.push_back(TheCondState);
2348 TheCondState.TheCond = AsmCond::IfCond;
2349
2350 if (TheCondState.Ignore) {
2351 EatToEndOfStatement();
2352 } else {
2353 if (ParseIdentifier(Name))
2354 return TokError("expected identifier after '.ifdef'");
2355
2356 Lex();
2357
2358 MCSymbol *Sym = getContext().LookupSymbol(Name);
2359
2360 if (expect_defined)
2361 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2362 else
2363 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2364 TheCondState.Ignore = !TheCondState.CondMet;
2365 }
2366
2367 return false;
2368}
2369
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002370/// ParseDirectiveElseIf
2371/// ::= .elseif expression
2372bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2373 if (TheCondState.TheCond != AsmCond::IfCond &&
2374 TheCondState.TheCond != AsmCond::ElseIfCond)
2375 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2376 " an .elseif");
2377 TheCondState.TheCond = AsmCond::ElseIfCond;
2378
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002379 bool LastIgnoreState = false;
2380 if (!TheCondStack.empty())
2381 LastIgnoreState = TheCondStack.back().Ignore;
2382 if (LastIgnoreState || TheCondState.CondMet) {
2383 TheCondState.Ignore = true;
2384 EatToEndOfStatement();
2385 }
2386 else {
2387 int64_t ExprValue;
2388 if (ParseAbsoluteExpression(ExprValue))
2389 return true;
2390
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002391 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002392 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002393
Sean Callanan79ed1a82010-01-19 20:22:31 +00002394 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002395 TheCondState.CondMet = ExprValue;
2396 TheCondState.Ignore = !TheCondState.CondMet;
2397 }
2398
2399 return false;
2400}
2401
2402/// ParseDirectiveElse
2403/// ::= .else
2404bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002405 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002406 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002407
Sean Callanan79ed1a82010-01-19 20:22:31 +00002408 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002409
2410 if (TheCondState.TheCond != AsmCond::IfCond &&
2411 TheCondState.TheCond != AsmCond::ElseIfCond)
2412 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2413 ".elseif");
2414 TheCondState.TheCond = AsmCond::ElseCond;
2415 bool LastIgnoreState = false;
2416 if (!TheCondStack.empty())
2417 LastIgnoreState = TheCondStack.back().Ignore;
2418 if (LastIgnoreState || TheCondState.CondMet)
2419 TheCondState.Ignore = true;
2420 else
2421 TheCondState.Ignore = false;
2422
2423 return false;
2424}
2425
2426/// ParseDirectiveEndIf
2427/// ::= .endif
2428bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002429 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002430 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002431
Sean Callanan79ed1a82010-01-19 20:22:31 +00002432 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002433
2434 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2435 TheCondStack.empty())
2436 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2437 ".else");
2438 if (!TheCondStack.empty()) {
2439 TheCondState = TheCondStack.back();
2440 TheCondStack.pop_back();
2441 }
2442
2443 return false;
2444}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002445
2446/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002447/// ::= .file [number] filename
2448/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002449bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002450 // FIXME: I'm not sure what this is.
2451 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002452 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002453 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002454 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002455 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002456
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002457 if (FileNumber < 1)
2458 return TokError("file number less than one");
2459 }
2460
Daniel Dunbareceec052010-07-12 17:45:27 +00002461 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002462 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002463
Nick Lewycky44d798d2011-10-17 23:05:28 +00002464 // Usually the directory and filename together, otherwise just the directory.
2465 StringRef Path = getTok().getString();
2466 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002467 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002468
Nick Lewycky44d798d2011-10-17 23:05:28 +00002469 StringRef Directory;
2470 StringRef Filename;
2471 if (getLexer().is(AsmToken::String)) {
2472 if (FileNumber == -1)
2473 return TokError("explicit path specified, but no file number");
2474 Filename = getTok().getString();
2475 Filename = Filename.substr(1, Filename.size()-2);
2476 Directory = Path;
2477 Lex();
2478 } else {
2479 Filename = Path;
2480 }
2481
Daniel Dunbareceec052010-07-12 17:45:27 +00002482 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002483 return TokError("unexpected token in '.file' directive");
2484
Chris Lattnerd32e8032010-01-25 19:02:58 +00002485 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002486 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002487 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002488 if (getContext().getGenDwarfForAssembly() == true)
2489 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2490 "used to generate dwarf debug info for assembly code");
2491
Nick Lewycky44d798d2011-10-17 23:05:28 +00002492 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002493 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002494 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002495
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002496 return false;
2497}
2498
2499/// ParseDirectiveLine
2500/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002501bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002502 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2503 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002504 return TokError("unexpected token in '.line' directive");
2505
Sean Callanan18b83232010-01-19 21:44:56 +00002506 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002507 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002508 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002509
2510 // FIXME: Do something with the .line.
2511 }
2512
Daniel Dunbareceec052010-07-12 17:45:27 +00002513 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002514 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002515
2516 return false;
2517}
2518
2519
2520/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002521/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002522/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2523/// The first number is a file number, must have been previously assigned with
2524/// a .file directive, the second number is the line number and optionally the
2525/// third number is a column position (zero if not specified). The remaining
2526/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002527bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002528
Daniel Dunbareceec052010-07-12 17:45:27 +00002529 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002530 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002531 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002532 if (FileNumber < 1)
2533 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002534 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002535 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002536 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002537
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002538 int64_t LineNumber = 0;
2539 if (getLexer().is(AsmToken::Integer)) {
2540 LineNumber = getTok().getIntVal();
2541 if (LineNumber < 1)
2542 return TokError("line number less than one in '.loc' directive");
2543 Lex();
2544 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002545
2546 int64_t ColumnPos = 0;
2547 if (getLexer().is(AsmToken::Integer)) {
2548 ColumnPos = getTok().getIntVal();
2549 if (ColumnPos < 0)
2550 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002551 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002552 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002553
Kevin Enderbyc0957932010-09-30 16:52:03 +00002554 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002555 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002556 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002557 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2558 for (;;) {
2559 if (getLexer().is(AsmToken::EndOfStatement))
2560 break;
2561
2562 StringRef Name;
2563 SMLoc Loc = getTok().getLoc();
2564 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002565 return TokError("unexpected token in '.loc' directive");
2566
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002567 if (Name == "basic_block")
2568 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2569 else if (Name == "prologue_end")
2570 Flags |= DWARF2_FLAG_PROLOGUE_END;
2571 else if (Name == "epilogue_begin")
2572 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2573 else if (Name == "is_stmt") {
2574 SMLoc Loc = getTok().getLoc();
2575 const MCExpr *Value;
2576 if (getParser().ParseExpression(Value))
2577 return true;
2578 // The expression must be the constant 0 or 1.
2579 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2580 int Value = MCE->getValue();
2581 if (Value == 0)
2582 Flags &= ~DWARF2_FLAG_IS_STMT;
2583 else if (Value == 1)
2584 Flags |= DWARF2_FLAG_IS_STMT;
2585 else
2586 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002587 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002588 else {
2589 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2590 }
2591 }
2592 else if (Name == "isa") {
2593 SMLoc Loc = getTok().getLoc();
2594 const MCExpr *Value;
2595 if (getParser().ParseExpression(Value))
2596 return true;
2597 // The expression must be a constant greater or equal to 0.
2598 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2599 int Value = MCE->getValue();
2600 if (Value < 0)
2601 return Error(Loc, "isa number less than zero");
2602 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002603 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002604 else {
2605 return Error(Loc, "isa number not a constant value");
2606 }
2607 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002608 else if (Name == "discriminator") {
2609 if (getParser().ParseAbsoluteExpression(Discriminator))
2610 return true;
2611 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002612 else {
2613 return Error(Loc, "unknown sub-directive in '.loc' directive");
2614 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002615
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002616 if (getLexer().is(AsmToken::EndOfStatement))
2617 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002618 }
2619 }
2620
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002621 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002622 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002623
2624 return false;
2625}
2626
Daniel Dunbar138abae2010-10-16 04:56:42 +00002627/// ParseDirectiveStabs
2628/// ::= .stabs string, number, number, number
2629bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2630 SMLoc DirectiveLoc) {
2631 return TokError("unsupported directive '" + Directive + "'");
2632}
2633
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002634/// ParseDirectiveCFISections
2635/// ::= .cfi_sections section [, section]
2636bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2637 SMLoc DirectiveLoc) {
2638 StringRef Name;
2639 bool EH = false;
2640 bool Debug = false;
2641
2642 if (getParser().ParseIdentifier(Name))
2643 return TokError("Expected an identifier");
2644
2645 if (Name == ".eh_frame")
2646 EH = true;
2647 else if (Name == ".debug_frame")
2648 Debug = true;
2649
2650 if (getLexer().is(AsmToken::Comma)) {
2651 Lex();
2652
2653 if (getParser().ParseIdentifier(Name))
2654 return TokError("Expected an identifier");
2655
2656 if (Name == ".eh_frame")
2657 EH = true;
2658 else if (Name == ".debug_frame")
2659 Debug = true;
2660 }
2661
2662 getStreamer().EmitCFISections(EH, Debug);
2663
2664 return false;
2665}
2666
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002667/// ParseDirectiveCFIStartProc
2668/// ::= .cfi_startproc
2669bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2670 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002671 getStreamer().EmitCFIStartProc();
2672 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002673}
2674
2675/// ParseDirectiveCFIEndProc
2676/// ::= .cfi_endproc
2677bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002678 getStreamer().EmitCFIEndProc();
2679 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002680}
2681
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002682/// ParseRegisterOrRegisterNumber - parse register name or number.
2683bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2684 SMLoc DirectiveLoc) {
2685 unsigned RegNo;
2686
Jim Grosbach6f888a82011-06-02 17:14:04 +00002687 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002688 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2689 DirectiveLoc))
2690 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002691 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002692 } else
2693 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002694
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002695 return false;
2696}
2697
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002698/// ParseDirectiveCFIDefCfa
2699/// ::= .cfi_def_cfa register, offset
2700bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2701 SMLoc DirectiveLoc) {
2702 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002703 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002704 return true;
2705
2706 if (getLexer().isNot(AsmToken::Comma))
2707 return TokError("unexpected token in directive");
2708 Lex();
2709
2710 int64_t Offset = 0;
2711 if (getParser().ParseAbsoluteExpression(Offset))
2712 return true;
2713
Rafael Espindola066c2f42011-04-12 23:59:07 +00002714 getStreamer().EmitCFIDefCfa(Register, Offset);
2715 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002716}
2717
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002718/// ParseDirectiveCFIDefCfaOffset
2719/// ::= .cfi_def_cfa_offset offset
2720bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2721 SMLoc DirectiveLoc) {
2722 int64_t Offset = 0;
2723 if (getParser().ParseAbsoluteExpression(Offset))
2724 return true;
2725
Rafael Espindola066c2f42011-04-12 23:59:07 +00002726 getStreamer().EmitCFIDefCfaOffset(Offset);
2727 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002728}
2729
2730/// ParseDirectiveCFIAdjustCfaOffset
2731/// ::= .cfi_adjust_cfa_offset adjustment
2732bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2733 SMLoc DirectiveLoc) {
2734 int64_t Adjustment = 0;
2735 if (getParser().ParseAbsoluteExpression(Adjustment))
2736 return true;
2737
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002738 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2739 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002740}
2741
2742/// ParseDirectiveCFIDefCfaRegister
2743/// ::= .cfi_def_cfa_register register
2744bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2745 SMLoc DirectiveLoc) {
2746 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002747 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002748 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002749
Rafael Espindola066c2f42011-04-12 23:59:07 +00002750 getStreamer().EmitCFIDefCfaRegister(Register);
2751 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002752}
2753
2754/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002755/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002756bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2757 int64_t Register = 0;
2758 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002759
2760 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002761 return true;
2762
2763 if (getLexer().isNot(AsmToken::Comma))
2764 return TokError("unexpected token in directive");
2765 Lex();
2766
2767 if (getParser().ParseAbsoluteExpression(Offset))
2768 return true;
2769
Rafael Espindola066c2f42011-04-12 23:59:07 +00002770 getStreamer().EmitCFIOffset(Register, Offset);
2771 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002772}
2773
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002774/// ParseDirectiveCFIRelOffset
2775/// ::= .cfi_rel_offset register, offset
2776bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2777 SMLoc DirectiveLoc) {
2778 int64_t Register = 0;
2779
2780 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2781 return true;
2782
2783 if (getLexer().isNot(AsmToken::Comma))
2784 return TokError("unexpected token in directive");
2785 Lex();
2786
2787 int64_t Offset = 0;
2788 if (getParser().ParseAbsoluteExpression(Offset))
2789 return true;
2790
Rafael Espindola25f492e2011-04-12 16:12:03 +00002791 getStreamer().EmitCFIRelOffset(Register, Offset);
2792 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002793}
2794
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002795static bool isValidEncoding(int64_t Encoding) {
2796 if (Encoding & ~0xff)
2797 return false;
2798
2799 if (Encoding == dwarf::DW_EH_PE_omit)
2800 return true;
2801
2802 const unsigned Format = Encoding & 0xf;
2803 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2804 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2805 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2806 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2807 return false;
2808
Rafael Espindolacaf11582010-12-29 04:31:26 +00002809 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002810 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002811 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002812 return false;
2813
2814 return true;
2815}
2816
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002817/// ParseDirectiveCFIPersonalityOrLsda
2818/// ::= .cfi_personality encoding, [symbol_name]
2819/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002820bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002821 SMLoc DirectiveLoc) {
2822 int64_t Encoding = 0;
2823 if (getParser().ParseAbsoluteExpression(Encoding))
2824 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002825 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002826 return false;
2827
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002828 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002829 return TokError("unsupported encoding.");
2830
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002831 if (getLexer().isNot(AsmToken::Comma))
2832 return TokError("unexpected token in directive");
2833 Lex();
2834
2835 StringRef Name;
2836 if (getParser().ParseIdentifier(Name))
2837 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002838
2839 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2840
2841 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002842 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002843 else {
2844 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002845 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002846 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002847 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002848}
2849
Rafael Espindolafe024d02010-12-28 18:36:23 +00002850/// ParseDirectiveCFIRememberState
2851/// ::= .cfi_remember_state
2852bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2853 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002854 getStreamer().EmitCFIRememberState();
2855 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002856}
2857
2858/// ParseDirectiveCFIRestoreState
2859/// ::= .cfi_remember_state
2860bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2861 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002862 getStreamer().EmitCFIRestoreState();
2863 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002864}
2865
Rafael Espindolac5754392011-04-12 15:31:05 +00002866/// ParseDirectiveCFISameValue
2867/// ::= .cfi_same_value register
2868bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2869 SMLoc DirectiveLoc) {
2870 int64_t Register = 0;
2871
2872 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2873 return true;
2874
2875 getStreamer().EmitCFISameValue(Register);
2876
2877 return false;
2878}
2879
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00002880/// ParseDirectiveCFIRestore
2881/// ::= .cfi_restore register
2882bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
2883 SMLoc DirectiveLoc) {
2884 int64_t Register = 0;
2885 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2886 return true;
2887
2888 getStreamer().EmitCFIRestore(Register);
2889
2890 return false;
2891}
2892
Rafael Espindola6f0b1812011-12-29 20:24:47 +00002893/// ParseDirectiveCFIEscape
2894/// ::= .cfi_escape expression[,...]
2895bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
2896 SMLoc DirectiveLoc) {
2897 std::string Values;
2898 int64_t CurrValue;
2899 if (getParser().ParseAbsoluteExpression(CurrValue))
2900 return true;
2901
2902 Values.push_back((uint8_t)CurrValue);
2903
2904 while (getLexer().is(AsmToken::Comma)) {
2905 Lex();
2906
2907 if (getParser().ParseAbsoluteExpression(CurrValue))
2908 return true;
2909
2910 Values.push_back((uint8_t)CurrValue);
2911 }
2912
2913 getStreamer().EmitCFIEscape(Values);
2914 return false;
2915}
2916
Rafael Espindola16d7d432012-01-23 21:51:52 +00002917/// ParseDirectiveCFISignalFrame
2918/// ::= .cfi_signal_frame
2919bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
2920 SMLoc DirectiveLoc) {
2921 if (getLexer().isNot(AsmToken::EndOfStatement))
2922 return Error(getLexer().getLoc(),
2923 "unexpected token in '" + Directive + "' directive");
2924
2925 getStreamer().EmitCFISignalFrame();
2926
2927 return false;
2928}
2929
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002930/// ParseDirectiveMacrosOnOff
2931/// ::= .macros_on
2932/// ::= .macros_off
2933bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2934 SMLoc DirectiveLoc) {
2935 if (getLexer().isNot(AsmToken::EndOfStatement))
2936 return Error(getLexer().getLoc(),
2937 "unexpected token in '" + Directive + "' directive");
2938
2939 getParser().MacrosEnabled = Directive == ".macros_on";
2940
2941 return false;
2942}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002943
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002944/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002945/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002946bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2947 SMLoc DirectiveLoc) {
2948 StringRef Name;
2949 if (getParser().ParseIdentifier(Name))
2950 return TokError("expected identifier in directive");
2951
Rafael Espindola65366442011-06-05 02:43:45 +00002952 std::vector<StringRef> Parameters;
2953 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2954 for(;;) {
2955 StringRef Parameter;
2956 if (getParser().ParseIdentifier(Parameter))
2957 return TokError("expected identifier in directive");
2958 Parameters.push_back(Parameter);
2959
2960 if (getLexer().isNot(AsmToken::Comma))
2961 break;
2962 Lex();
2963 }
2964 }
2965
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002966 if (getLexer().isNot(AsmToken::EndOfStatement))
2967 return TokError("unexpected token in '.macro' directive");
2968
2969 // Eat the end of statement.
2970 Lex();
2971
2972 AsmToken EndToken, StartToken = getTok();
2973
2974 // Lex the macro definition.
2975 for (;;) {
2976 // Check whether we have reached the end of the file.
2977 if (getLexer().is(AsmToken::Eof))
2978 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2979
2980 // Otherwise, check whether we have reach the .endmacro.
2981 if (getLexer().is(AsmToken::Identifier) &&
2982 (getTok().getIdentifier() == ".endm" ||
2983 getTok().getIdentifier() == ".endmacro")) {
2984 EndToken = getTok();
2985 Lex();
2986 if (getLexer().isNot(AsmToken::EndOfStatement))
2987 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2988 "' directive");
2989 break;
2990 }
2991
2992 // Otherwise, scan til the end of the statement.
2993 getParser().EatToEndOfStatement();
2994 }
2995
2996 if (getParser().MacroMap.lookup(Name)) {
2997 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2998 }
2999
3000 const char *BodyStart = StartToken.getLoc().getPointer();
3001 const char *BodyEnd = EndToken.getLoc().getPointer();
3002 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003003 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003004 return false;
3005}
3006
3007/// ParseDirectiveEndMacro
3008/// ::= .endm
3009/// ::= .endmacro
3010bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
3011 SMLoc DirectiveLoc) {
3012 if (getLexer().isNot(AsmToken::EndOfStatement))
3013 return TokError("unexpected token in '" + Directive + "' directive");
3014
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003015 // If we are inside a macro instantiation, terminate the current
3016 // instantiation.
3017 if (!getParser().ActiveMacros.empty()) {
3018 getParser().HandleMacroExit();
3019 return false;
3020 }
3021
3022 // Otherwise, this .endmacro is a stray entry in the file; well formed
3023 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003024 return TokError("unexpected '" + Directive + "' in file, "
3025 "no current macro definition");
3026}
3027
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003028bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003029 getParser().CheckForValidSection();
3030
3031 const MCExpr *Value;
3032
3033 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003034 return true;
3035
3036 if (getLexer().isNot(AsmToken::EndOfStatement))
3037 return TokError("unexpected token in directive");
3038
3039 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003040 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003041 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003042 getStreamer().EmitULEB128Value(Value);
3043
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003044 return false;
3045}
3046
3047
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003048/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003049MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003050 MCContext &C, MCStreamer &Out,
3051 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003052 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003053}