blob: 690684f50ae28752f011211b1bcaa802d6db5757 [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"
Chad Rosierb1f8c132012-10-18 15:49:34 +000022#include "llvm/MC/MCInstPrinter.h"
23#include "llvm/MC/MCInstrInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000024#include "llvm/MC/MCParser/AsmCond.h"
25#include "llvm/MC/MCParser/AsmLexer.h"
26#include "llvm/MC/MCParser/MCAsmParser.h"
27#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000028#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000029#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000030#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000031#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000032#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000033#include "llvm/Support/CommandLine.h"
Benjamin Kramer518ff562012-01-28 15:28:41 +000034#include "llvm/Support/ErrorHandling.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000035#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000036#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000037#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000038#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000039#include <cctype>
Chad Rosierb1f8c132012-10-18 15:49:34 +000040#include <set>
41#include <string>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000042#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000043using namespace llvm;
44
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000045static cl::opt<bool>
46FatalAssemblerWarnings("fatal-assembler-warnings",
47 cl::desc("Consider warnings as error"));
48
Nick Lewycky0d7d11d2012-10-19 07:00:09 +000049MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
50
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000051namespace {
52
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000053/// \brief Helper class for tracking macro definitions.
Rafael Espindola28c1f6662012-06-03 22:41:23 +000054typedef std::vector<AsmToken> MacroArgument;
Rafael Espindola8a403d32012-08-08 14:51:03 +000055typedef std::vector<MacroArgument> MacroArguments;
Preston Gurd6c9176a2012-09-19 20:29:04 +000056typedef std::pair<StringRef, MacroArgument> MacroParameter;
Rafael Espindola8a403d32012-08-08 14:51:03 +000057typedef std::vector<MacroParameter> MacroParameters;
Rafael Espindola28c1f6662012-06-03 22:41:23 +000058
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000059struct Macro {
60 StringRef Name;
61 StringRef Body;
Rafael Espindola8a403d32012-08-08 14:51:03 +000062 MacroParameters Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000063
64public:
Rafael Espindola8a403d32012-08-08 14:51:03 +000065 Macro(StringRef N, StringRef B, const MacroParameters &P) :
Rafael Espindola65366442011-06-05 02:43:45 +000066 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000067};
68
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000069/// \brief Helper class for storing information about an active macro
70/// instantiation.
71struct MacroInstantiation {
72 /// The macro being instantiated.
73 const Macro *TheMacro;
74
75 /// The macro instantiation with substitutions.
76 MemoryBuffer *Instantiation;
77
78 /// The location of the instantiation.
79 SMLoc InstantiationLoc;
80
81 /// The location where parsing should resume upon instantiation completion.
82 SMLoc ExitLoc;
83
84public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000085 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000086 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000087};
88
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089/// \brief The concrete assembly parser instance.
90class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000091 friend class GenericAsmParser;
92
Craig Topper85aadc02012-09-15 16:23:52 +000093 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
94 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000095private:
96 AsmLexer Lexer;
97 MCContext &Ctx;
98 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000099 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000100 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +0000101 SourceMgr::DiagHandlerTy SavedDiagHandler;
102 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000103 MCAsmParserExtension *GenericParser;
104 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000105
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000106 /// This is the current buffer index we're lexing from as managed by the
107 /// SourceMgr object.
108 int CurBuffer;
109
110 AsmCond TheCondState;
111 std::vector<AsmCond> TheCondStack;
112
113 /// DirectiveMap - This is a table handlers for directives. Each handler is
114 /// invoked after the directive identifier is read and is responsible for
115 /// parsing and validating the rest of the directive. The handler is passed
116 /// in the directive name and the location of the directive keyword.
117 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000118
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000119 /// MacroMap - Map of currently defined macros.
120 StringMap<Macro*> MacroMap;
121
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000122 /// ActiveMacros - Stack of active macro instantiations.
123 std::vector<MacroInstantiation*> ActiveMacros;
124
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000125 /// Boolean tracking whether macro substitution is enabled.
126 unsigned MacrosEnabled : 1;
127
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000128 /// Flag tracking whether any errors have been encountered.
129 unsigned HadError : 1;
130
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000131 /// The values from the last parsed cpp hash file line comment if any.
132 StringRef CppHashFilename;
133 int64_t CppHashLineNumber;
134 SMLoc CppHashLoc;
135
Devang Patel0db58bf2012-01-31 18:14:05 +0000136 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
137 unsigned AssemblerDialect;
138
Preston Gurd7b6f2032012-09-19 20:36:12 +0000139 /// IsDarwin - is Darwin compatibility enabled?
140 bool IsDarwin;
141
Chad Rosier8f138d12012-10-15 17:19:13 +0000142 /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
Chad Rosier84125ca2012-10-13 00:26:04 +0000143 bool ParsingInlineAsm;
144
Chad Rosier8f138d12012-10-15 17:19:13 +0000145 /// ParsedOperands - The parsed operands from the last parsed statement.
146 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
147
Chad Rosierb1f8c132012-10-18 15:49:34 +0000148 /// Opcode - The opcode from the last parsed instruction. This is MS-style
149 /// inline asm specific.
Chad Rosier8f138d12012-10-15 17:19:13 +0000150 unsigned Opcode;
151
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000152public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000153 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000154 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000155 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000156
157 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
158
Craig Topper345d16d2012-08-29 05:48:09 +0000159 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
160 StringRef Directive,
161 DirectiveHandler Handler) {
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000162 DirectiveMap[Directive] = std::make_pair(Object, Handler);
163 }
164
165public:
166 /// @name MCAsmParser Interface
167 /// {
168
169 virtual SourceMgr &getSourceManager() { return SrcMgr; }
170 virtual MCAsmLexer &getLexer() { return Lexer; }
171 virtual MCContext &getContext() { return Ctx; }
172 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000173 virtual unsigned getAssemblerDialect() {
174 if (AssemblerDialect == ~0U)
175 return MAI.getAssemblerDialect();
176 else
177 return AssemblerDialect;
178 }
179 virtual void setAssemblerDialect(unsigned i) {
180 AssemblerDialect = i;
181 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000182
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000183 virtual bool Warning(SMLoc L, const Twine &Msg,
184 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
185 virtual bool Error(SMLoc L, const Twine &Msg,
186 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000187
Craig Topper345d16d2012-08-29 05:48:09 +0000188 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000189
Chad Rosier84125ca2012-10-13 00:26:04 +0000190 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosierc5ac87d2012-10-16 20:16:20 +0000191 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosierb1f8c132012-10-18 15:49:34 +0000192
193 bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
194 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosierc8dd27e2012-10-18 19:39:30 +0000195 SmallVectorImpl<void *> &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000196 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000197 SmallVectorImpl<std::string> &Clobbers,
198 const MCInstrInfo *MII,
199 const MCInstPrinter *IP,
200 MCAsmParserSemaCallback &SI);
Chad Rosier84125ca2012-10-13 00:26:04 +0000201
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000202 bool ParseExpression(const MCExpr *&Res);
203 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
204 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
205 virtual bool ParseAbsoluteExpression(int64_t &Res);
206
207 /// }
208
209private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000210 void CheckForValidSection();
211
Chad Rosierb1f8c132012-10-18 15:49:34 +0000212 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000213 void EatToEndOfLine();
214 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000215
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000216 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000217 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +0000218 const MacroParameters &Parameters,
219 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000220 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000221 void HandleMacroExit();
222
223 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000224 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000225 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
226 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000227 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000228 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000229
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000230 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
231 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000232 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
233 /// This returns true on failure.
234 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000235
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000236 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000237 /// current token is not set; clients should ensure Lex() is called
238 /// subsequently.
239 void JumpToLoc(SMLoc Loc);
240
Craig Topper345d16d2012-08-29 05:48:09 +0000241 virtual void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000242
Preston Gurd7b6f2032012-09-19 20:36:12 +0000243 bool ParseMacroArgument(MacroArgument &MA,
244 AsmToken::TokenKind &ArgumentDelimiter);
Rafael Espindola8a403d32012-08-08 14:51:03 +0000245 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000246
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000247 /// \brief Parse up to the end of statement and a return the contents from the
248 /// current token until the end of the statement; the current token on exit
249 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000250 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000251
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000252 /// \brief Parse until the end of a statement or a comma is encountered,
253 /// return the contents from the current token up to the end or comma.
254 StringRef ParseStringToComma();
255
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000256 bool ParseAssignment(StringRef Name, bool allow_redef,
257 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000258
259 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
260 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
261 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000262 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000263
264 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000265 /// and set \p Res to the identifier contents.
Craig Topper345d16d2012-08-29 05:48:09 +0000266 virtual bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000267
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000268 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000269
270 // ".ascii", ".asciiz", ".string"
271 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000272 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000273 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000274 bool ParseDirectiveFill(); // ".fill"
275 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000276 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000277 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000278 bool ParseDirectiveOrg(); // ".org"
279 // ".align{,32}", ".p2align{,w,l}"
280 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
281
282 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
283 /// accepts a single symbol (which should be a label or an external).
284 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000285
286 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
287
288 bool ParseDirectiveAbort(); // ".abort"
289 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000290 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000291
292 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000293 // ".ifb" or ".ifnb", depending on ExpectBlank.
294 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000295 // ".ifc" or ".ifnc", depending on ExpectEqual.
296 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000297 // ".ifdef" or ".ifndef", depending on expect_defined
298 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000299 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
300 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
301 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
302
303 /// ParseEscapedString - Parse the current token as a string which may include
304 /// escaped characters and return the string contents.
305 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000306
307 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
308 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000309
Rafael Espindola761cb062012-06-03 23:57:14 +0000310 // Macro-like directives
311 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
312 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
313 raw_svector_ostream &OS);
314 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000315 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000316 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000317 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosierb1f8c132012-10-18 15:49:34 +0000318
319 // MS-style inline assembly parsing.
320 bool isInstruction() { return Opcode != (unsigned)~0x0; }
321 unsigned getOpcode() { return Opcode; }
Chad Rosierab450e42012-10-19 22:57:33 +0000322 void setOpcode(unsigned Value) { Opcode = Value; }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000323};
324
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000325/// \brief Generic implementations of directive handling, etc. which is shared
326/// (or the default, at least) for all assembler parser.
327class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000328 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
329 void AddDirectiveHandler(StringRef Directive) {
330 getParser().AddDirectiveHandler(this, Directive,
331 HandleDirective<GenericAsmParser, Handler>);
332 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000333public:
334 GenericAsmParser() {}
335
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000336 AsmParser &getParser() {
337 return (AsmParser&) this->MCAsmParserExtension::getParser();
338 }
339
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000340 virtual void Initialize(MCAsmParser &Parser) {
341 // Call the base implementation.
342 this->MCAsmParserExtension::Initialize(Parser);
343
344 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000345 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
346 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
347 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000348 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000349
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000350 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000351 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
352 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000353 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
354 ".cfi_startproc");
355 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
356 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000357 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
358 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000359 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
360 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000361 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
362 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000363 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
364 ".cfi_def_cfa_register");
365 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
366 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000367 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
368 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000369 AddDirectiveHandler<
370 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
371 AddDirectiveHandler<
372 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000373 AddDirectiveHandler<
374 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
375 AddDirectiveHandler<
376 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000377 AddDirectiveHandler<
378 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000379 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000380 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
381 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000382 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000383 AddDirectiveHandler<
384 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000385
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000386 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000387 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
388 ".macros_on");
389 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
390 ".macros_off");
391 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
392 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
393 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000394 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000395
396 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
397 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000398 }
399
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000400 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
401
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000402 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
403 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
404 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000405 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000406 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000407 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
408 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000409 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000410 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000411 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000412 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
413 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000414 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000415 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000416 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
417 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000418 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000419 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000420 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000421 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000422
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000423 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000424 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
425 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000426 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000427
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000428 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000429};
430
431}
432
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000433namespace llvm {
434
435extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000436extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000437extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000438
439}
440
Chris Lattneraaec2052010-01-19 19:46:13 +0000441enum { DEFAULT_ADDRSPACE = 0 };
442
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000443AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000444 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000445 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000446 GenericParser(new GenericAsmParser), PlatformParser(0),
Preston Gurd7b6f2032012-09-19 20:36:12 +0000447 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
Chad Rosier8f138d12012-10-15 17:19:13 +0000448 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false),
Chad Rosier127f5ed2012-10-15 19:08:18 +0000449 Opcode(~0x0) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000450 // Save the old handler.
451 SavedDiagHandler = SrcMgr.getDiagHandler();
452 SavedDiagContext = SrcMgr.getDiagContext();
453 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000454 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000455 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000456
457 // Initialize the generic parser.
458 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000459
460 // Initialize the platform / file format parser.
461 //
462 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
463 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000464 if (_MAI.hasMicrosoftFastStdCallMangling()) {
465 PlatformParser = createCOFFAsmParser();
466 PlatformParser->Initialize(*this);
467 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000468 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000469 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000470 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000471 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000472 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000473 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000474 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000475}
476
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000477AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000478 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
479
480 // Destroy any macros.
481 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
482 ie = MacroMap.end(); it != ie; ++it)
483 delete it->getValue();
484
Daniel Dunbare4749702010-07-12 18:12:02 +0000485 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000486 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000487}
488
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000489void AsmParser::PrintMacroInstantiations() {
490 // Print the active macro instantiation stack.
491 for (std::vector<MacroInstantiation*>::const_reverse_iterator
492 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000493 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
494 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000495}
496
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000497bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000498 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000499 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000500 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000501 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000502 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000503}
504
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000505bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000506 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000507 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000508 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000509 return true;
510}
511
Sean Callananfd0b0282010-01-21 00:19:58 +0000512bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000513 std::string IncludedFile;
514 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000515 if (NewBuf == -1)
516 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000517
Sean Callananfd0b0282010-01-21 00:19:58 +0000518 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000519
Sean Callananfd0b0282010-01-21 00:19:58 +0000520 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000521
Sean Callananfd0b0282010-01-21 00:19:58 +0000522 return false;
523}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000524
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000525/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000526/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000527/// returns true on failure.
528bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
529 std::string IncludedFile;
530 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
531 if (NewBuf == -1)
532 return true;
533
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000534 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000535 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
536 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000537 return false;
538}
539
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000540void AsmParser::JumpToLoc(SMLoc Loc) {
541 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
542 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
543}
544
Sean Callananfd0b0282010-01-21 00:19:58 +0000545const AsmToken &AsmParser::Lex() {
546 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000547
Sean Callananfd0b0282010-01-21 00:19:58 +0000548 if (tok->is(AsmToken::Eof)) {
549 // If this is the end of an included file, pop the parent file off the
550 // include stack.
551 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
552 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000553 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000554 tok = &Lexer.Lex();
555 }
556 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000557
Sean Callananfd0b0282010-01-21 00:19:58 +0000558 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000559 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000560
Sean Callananfd0b0282010-01-21 00:19:58 +0000561 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000562}
563
Chris Lattner79180e22010-04-05 23:15:42 +0000564bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000565 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000566 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000567 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000568
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000569 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000570 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000571
572 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000573 AsmCond StartingCondState = TheCondState;
574
Kevin Enderby613b7572011-11-01 22:27:22 +0000575 // If we are generating dwarf for assembly source files save the initial text
576 // section and generate a .file directive.
577 if (getContext().getGenDwarfForAssembly()) {
578 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000579 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
580 getStreamer().EmitLabel(SectionStartSym);
581 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000582 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
583 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
584 }
585
Chris Lattnerb717fb02009-07-02 21:53:43 +0000586 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000587 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000588 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000589
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000590 // We had an error, validate that one was emitted and recover by skipping to
591 // the next line.
592 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000593 EatToEndOfStatement();
594 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000595
596 if (TheCondState.TheCond != StartingCondState.TheCond ||
597 TheCondState.Ignore != StartingCondState.Ignore)
598 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000599
600 // Check to see there are no empty DwarfFile slots.
601 const std::vector<MCDwarfFile *> &MCDwarfFiles =
602 getContext().getMCDwarfFiles();
603 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000604 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000605 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000606 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000607
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000608 // Check to see that all assembler local symbols were actually defined.
609 // Targets that don't do subsections via symbols may not want this, though,
610 // so conservatively exclude them. Only do this if we're finalizing, though,
611 // as otherwise we won't necessarilly have seen everything yet.
612 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
613 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
614 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
615 e = Symbols.end();
616 i != e; ++i) {
617 MCSymbol *Sym = i->getValue();
618 // Variable symbols may not be marked as defined, so check those
619 // explicitly. If we know it's a variable, we have a definition for
620 // the purposes of this check.
621 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
622 // FIXME: We would really like to refer back to where the symbol was
623 // first referenced for a source location. We need to add something
624 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000625 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
626 "assembler local symbol '" + Sym->getName() +
627 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000628 }
629 }
630
631
Chris Lattner79180e22010-04-05 23:15:42 +0000632 // Finalize the output stream if there are no errors and if the client wants
633 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000634 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000635 Out.Finish();
636
Chris Lattnerb717fb02009-07-02 21:53:43 +0000637 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000638}
639
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000640void AsmParser::CheckForValidSection() {
Chad Rosier84125ca2012-10-13 00:26:04 +0000641 if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000642 TokError("expected section directive before assembly directive");
643 Out.SwitchSection(Ctx.getMachOSection(
644 "__TEXT", "__text",
645 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
646 0, SectionKind::getText()));
647 }
648}
649
Chris Lattner2cf5f142009-06-22 01:29:09 +0000650/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
651void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000652 while (Lexer.isNot(AsmToken::EndOfStatement) &&
653 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000654 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000655
Chris Lattner2cf5f142009-06-22 01:29:09 +0000656 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000657 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000658 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000659}
660
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000661StringRef AsmParser::ParseStringToEndOfStatement() {
662 const char *Start = getTok().getLoc().getPointer();
663
664 while (Lexer.isNot(AsmToken::EndOfStatement) &&
665 Lexer.isNot(AsmToken::Eof))
666 Lex();
667
668 const char *End = getTok().getLoc().getPointer();
669 return StringRef(Start, End - Start);
670}
Chris Lattnerc4193832009-06-22 05:51:26 +0000671
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000672StringRef AsmParser::ParseStringToComma() {
673 const char *Start = getTok().getLoc().getPointer();
674
675 while (Lexer.isNot(AsmToken::EndOfStatement) &&
676 Lexer.isNot(AsmToken::Comma) &&
677 Lexer.isNot(AsmToken::Eof))
678 Lex();
679
680 const char *End = getTok().getLoc().getPointer();
681 return StringRef(Start, End - Start);
682}
683
Chris Lattner74ec1a32009-06-22 06:32:03 +0000684/// ParseParenExpr - Parse a paren expression and return it.
685/// NOTE: This assumes the leading '(' has already been consumed.
686///
687/// parenexpr ::= expr)
688///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000689bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000690 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000691 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000692 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000693 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000694 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000695 return false;
696}
Chris Lattnerc4193832009-06-22 05:51:26 +0000697
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000698/// ParseBracketExpr - Parse a bracket expression and return it.
699/// NOTE: This assumes the leading '[' has already been consumed.
700///
701/// bracketexpr ::= expr]
702///
703bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
704 if (ParseExpression(Res)) return true;
705 if (Lexer.isNot(AsmToken::RBrac))
706 return TokError("expected ']' in brackets expression");
707 EndLoc = Lexer.getLoc();
708 Lex();
709 return false;
710}
711
Chris Lattner74ec1a32009-06-22 06:32:03 +0000712/// ParsePrimaryExpr - Parse a primary expression and return it.
713/// primaryexpr ::= (parenexpr
714/// primaryexpr ::= symbol
715/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000716/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000717/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000718bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000719 switch (Lexer.getKind()) {
720 default:
721 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000722 // If we have an error assume that we've already handled it.
723 case AsmToken::Error:
724 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000725 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000726 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000727 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000728 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000729 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000730 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000731 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000732 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000733 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000734 EndLoc = Lexer.getLoc();
735
736 StringRef Identifier;
737 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000738 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000739
Daniel Dunbarfffff912009-10-16 01:34:54 +0000740 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000741 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000742 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000743
744 // Lookup the symbol variant if used.
745 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000746 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000747 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000748 if (Variant == MCSymbolRefExpr::VK_Invalid) {
749 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000750 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000751 }
752 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000753
Daniel Dunbarfffff912009-10-16 01:34:54 +0000754 // If this is an absolute variable reference, substitute it now to preserve
755 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000756 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000757 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000758 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000759
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000760 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000761 return false;
762 }
763
764 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000765 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000766 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000767 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000768 case AsmToken::Integer: {
769 SMLoc Loc = getTok().getLoc();
770 int64_t IntVal = getTok().getIntVal();
771 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000772 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000773 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000774 // Look for 'b' or 'f' following an Integer as a directional label
775 if (Lexer.getKind() == AsmToken::Identifier) {
776 StringRef IDVal = getTok().getString();
777 if (IDVal == "f" || IDVal == "b"){
778 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
779 IDVal == "f" ? 1 : 0);
780 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
781 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000782 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000783 return Error(Loc, "invalid reference to undefined symbol");
784 EndLoc = Lexer.getLoc();
785 Lex(); // Eat identifier.
786 }
787 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000788 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000789 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000790 case AsmToken::Real: {
791 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000792 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000793 Res = MCConstantExpr::Create(IntVal, getContext());
794 Lex(); // Eat token.
795 return false;
796 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000797 case AsmToken::Dot: {
798 // This is a '.' reference, which references the current PC. Emit a
799 // temporary label to the streamer and refer to it.
800 MCSymbol *Sym = Ctx.CreateTempSymbol();
801 Out.EmitLabel(Sym);
802 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
803 EndLoc = Lexer.getLoc();
804 Lex(); // Eat identifier.
805 return false;
806 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000807 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000808 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000809 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000810 case AsmToken::LBrac:
811 if (!PlatformParser->HasBracketExpressions())
812 return TokError("brackets expression not supported on this target");
813 Lex(); // Eat the '['.
814 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000815 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000816 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000817 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000818 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000819 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000820 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000821 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000822 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000823 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000824 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000825 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000826 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000827 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000828 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000829 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000830 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000831 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000832 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000833 }
834}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000835
Chris Lattnerb4307b32010-01-15 19:28:38 +0000836bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000837 SMLoc EndLoc;
838 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000839}
840
Daniel Dunbarcceba832010-09-17 02:47:07 +0000841const MCExpr *
842AsmParser::ApplyModifierToExpr(const MCExpr *E,
843 MCSymbolRefExpr::VariantKind Variant) {
844 // Recurse over the given expression, rebuilding it to apply the given variant
845 // if there is exactly one symbol.
846 switch (E->getKind()) {
847 case MCExpr::Target:
848 case MCExpr::Constant:
849 return 0;
850
851 case MCExpr::SymbolRef: {
852 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
853
854 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
855 TokError("invalid variant on expression '" +
856 getTok().getIdentifier() + "' (already modified)");
857 return E;
858 }
859
860 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
861 }
862
863 case MCExpr::Unary: {
864 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
865 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
866 if (!Sub)
867 return 0;
868 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
869 }
870
871 case MCExpr::Binary: {
872 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
873 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
874 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
875
876 if (!LHS && !RHS)
877 return 0;
878
879 if (!LHS) LHS = BE->getLHS();
880 if (!RHS) RHS = BE->getRHS();
881
882 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
883 }
884 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000885
Craig Topper85814382012-02-07 05:05:23 +0000886 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000887}
888
Chris Lattner74ec1a32009-06-22 06:32:03 +0000889/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000890///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000891/// expr ::= expr &&,|| expr -> lowest.
892/// expr ::= expr |,^,&,! expr
893/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
894/// expr ::= expr <<,>> expr
895/// expr ::= expr +,- expr
896/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000897/// expr ::= primaryexpr
898///
Chris Lattner54482b42010-01-15 19:39:23 +0000899bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000900 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000901 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000902 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
903 return true;
904
Daniel Dunbarcceba832010-09-17 02:47:07 +0000905 // As a special case, we support 'a op b @ modifier' by rewriting the
906 // expression to include the modifier. This is inefficient, but in general we
907 // expect users to use 'a@modifier op b'.
908 if (Lexer.getKind() == AsmToken::At) {
909 Lex();
910
911 if (Lexer.isNot(AsmToken::Identifier))
912 return TokError("unexpected symbol modifier following '@'");
913
914 MCSymbolRefExpr::VariantKind Variant =
915 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
916 if (Variant == MCSymbolRefExpr::VK_Invalid)
917 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
918
919 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
920 if (!ModifiedRes) {
921 return TokError("invalid modifier '" + getTok().getIdentifier() +
922 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000923 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000924
Daniel Dunbarcceba832010-09-17 02:47:07 +0000925 Res = ModifiedRes;
926 Lex();
927 }
928
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000929 // Try to constant fold it up front, if possible.
930 int64_t Value;
931 if (Res->EvaluateAsAbsolute(Value))
932 Res = MCConstantExpr::Create(Value, getContext());
933
934 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000935}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000936
Chris Lattnerb4307b32010-01-15 19:28:38 +0000937bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000938 Res = 0;
939 return ParseParenExpr(Res, EndLoc) ||
940 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000941}
942
Daniel Dunbar475839e2009-06-29 20:37:27 +0000943bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000944 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000945
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000946 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000947 if (ParseExpression(Expr))
948 return true;
949
Daniel Dunbare00b0112009-10-16 01:57:52 +0000950 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000951 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000952
953 return false;
954}
955
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000956static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000957 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000958 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000959 default:
960 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000961
Jim Grosbachfbe16812011-08-20 16:24:13 +0000962 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000963 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000964 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000965 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000966 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000967 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000968 return 1;
969
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000970
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000971 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000972 //
973 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000974 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000975 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000976 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000977 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000978 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000979 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000980 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000981 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000982 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000983
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000984 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000985 case AsmToken::EqualEqual:
986 Kind = MCBinaryExpr::EQ;
987 return 3;
988 case AsmToken::ExclaimEqual:
989 case AsmToken::LessGreater:
990 Kind = MCBinaryExpr::NE;
991 return 3;
992 case AsmToken::Less:
993 Kind = MCBinaryExpr::LT;
994 return 3;
995 case AsmToken::LessEqual:
996 Kind = MCBinaryExpr::LTE;
997 return 3;
998 case AsmToken::Greater:
999 Kind = MCBinaryExpr::GT;
1000 return 3;
1001 case AsmToken::GreaterEqual:
1002 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001003 return 3;
1004
Jim Grosbachfbe16812011-08-20 16:24:13 +00001005 // Intermediate Precedence: <<, >>
1006 case AsmToken::LessLess:
1007 Kind = MCBinaryExpr::Shl;
1008 return 4;
1009 case AsmToken::GreaterGreater:
1010 Kind = MCBinaryExpr::Shr;
1011 return 4;
1012
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001013 // High Intermediate Precedence: +, -
1014 case AsmToken::Plus:
1015 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001016 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001017 case AsmToken::Minus:
1018 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001019 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001020
Jim Grosbachfbe16812011-08-20 16:24:13 +00001021 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +00001022 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001023 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001024 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001025 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001026 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001027 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001028 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001029 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001030 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001031 }
1032}
1033
1034
1035/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
1036/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001037bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1038 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001039 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001040 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001041 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001042
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001043 // If the next token is lower precedence than we are allowed to eat, return
1044 // successfully with what we ate already.
1045 if (TokPrec < Precedence)
1046 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001047
Sean Callanan79ed1a82010-01-19 20:22:31 +00001048 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001049
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001050 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001051 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001052 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001053
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001054 // If BinOp binds less tightly with RHS than the operator after RHS, let
1055 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001056 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001057 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001058 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001059 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001060 }
1061
Daniel Dunbar475839e2009-06-29 20:37:27 +00001062 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001063 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001064 }
1065}
1066
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001067/// ParseStatement:
1068/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001069/// ::= Label* Directive ...Operands... EndOfStatement
1070/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001071bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001072 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001073 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001074 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001075 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001076 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001077
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001078 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001079 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001080 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001081 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001082 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001083 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001084 if (Lexer.is(AsmToken::Hash))
1085 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001086
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001087 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001088 if (Lexer.is(AsmToken::Integer)) {
1089 LocalLabelVal = getTok().getIntVal();
1090 if (LocalLabelVal < 0) {
1091 if (!TheCondState.Ignore)
1092 return TokError("unexpected token at start of statement");
1093 IDVal = "";
1094 }
1095 else {
1096 IDVal = getTok().getString();
1097 Lex(); // Consume the integer token to be used as an identifier token.
1098 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001099 if (!TheCondState.Ignore)
1100 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001101 }
1102 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001103
1104 } else if (Lexer.is(AsmToken::Dot)) {
1105 // Treat '.' as a valid identifier in this context.
1106 Lex();
1107 IDVal = ".";
1108
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001109 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001110 if (!TheCondState.Ignore)
1111 return TokError("unexpected token at start of statement");
1112 IDVal = "";
1113 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001114
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001115
Chris Lattner7834fac2010-04-17 18:14:27 +00001116 // Handle conditional assembly here before checking for skipping. We
1117 // have to do this so that .endif isn't skipped in a ".if 0" block for
1118 // example.
1119 if (IDVal == ".if")
1120 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001121 if (IDVal == ".ifb")
1122 return ParseDirectiveIfb(IDLoc, true);
1123 if (IDVal == ".ifnb")
1124 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00001125 if (IDVal == ".ifc")
1126 return ParseDirectiveIfc(IDLoc, true);
1127 if (IDVal == ".ifnc")
1128 return ParseDirectiveIfc(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001129 if (IDVal == ".ifdef")
1130 return ParseDirectiveIfdef(IDLoc, true);
1131 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1132 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001133 if (IDVal == ".elseif")
1134 return ParseDirectiveElseIf(IDLoc);
1135 if (IDVal == ".else")
1136 return ParseDirectiveElse(IDLoc);
1137 if (IDVal == ".endif")
1138 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001139
Chris Lattner7834fac2010-04-17 18:14:27 +00001140 // If we are in a ".if 0" block, ignore this statement.
1141 if (TheCondState.Ignore) {
1142 EatToEndOfStatement();
1143 return false;
1144 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001145
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001146 // FIXME: Recurse on local labels?
1147
1148 // See what kind of statement we have.
1149 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001150 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001151 CheckForValidSection();
1152
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001153 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001154 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001155
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001156 // Diagnose attempt to use '.' as a label.
1157 if (IDVal == ".")
1158 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1159
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001160 // Diagnose attempt to use a variable as a label.
1161 //
1162 // FIXME: Diagnostics. Note the location of the definition as a label.
1163 // FIXME: This doesn't diagnose assignment to a symbol which has been
1164 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001165 MCSymbol *Sym;
1166 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001167 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001168 else
1169 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001170 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001171 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001172
Daniel Dunbar959fd882009-08-26 22:13:22 +00001173 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001174 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001175
Kevin Enderby94c2e852011-12-09 18:09:40 +00001176 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001177 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001178 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001179 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1180 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001181
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001182 // Consume any end of statement token, if present, to avoid spurious
1183 // AddBlankLine calls().
1184 if (Lexer.is(AsmToken::EndOfStatement)) {
1185 Lex();
1186 if (Lexer.is(AsmToken::Eof))
1187 return false;
1188 }
1189
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001190 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001191 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001192
Daniel Dunbar3f872332009-07-28 16:08:33 +00001193 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001194 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001195 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001196
Nico Weber4c4c7322011-01-28 03:04:41 +00001197 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001198
1199 default: // Normal instruction or directive.
1200 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001201 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001202
1203 // If macros are enabled, check to see if this is a macro instantiation.
1204 if (MacrosEnabled)
1205 if (const Macro *M = MacroMap.lookup(IDVal))
1206 return HandleMacroEntry(IDVal, IDLoc, M);
1207
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001208 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001209 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001210
1211 // Target hook for parsing target specific directives.
1212 if (!getTargetParser().ParseDirective(ID))
1213 return false;
1214
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001215 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001216 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001217 return ParseDirectiveSet(IDVal, true);
1218 if (IDVal == ".equiv")
1219 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001220
Daniel Dunbara0d14262009-06-24 23:30:00 +00001221 // Data directives
1222
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001223 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001224 return ParseDirectiveAscii(IDVal, false);
1225 if (IDVal == ".asciz" || IDVal == ".string")
1226 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001227
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001228 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001229 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001230 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001231 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001232 if (IDVal == ".value")
1233 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001234 if (IDVal == ".2byte")
1235 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001236 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001237 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001238 if (IDVal == ".int")
1239 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001240 if (IDVal == ".4byte")
1241 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001242 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001243 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001244 if (IDVal == ".8byte")
1245 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001246 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001247 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1248 if (IDVal == ".double")
1249 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001250
Eli Friedman5d68ec22010-07-19 04:17:25 +00001251 if (IDVal == ".align") {
1252 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1253 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1254 }
1255 if (IDVal == ".align32") {
1256 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1257 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1258 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001259 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001260 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001261 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001262 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001263 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001264 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001265 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001266 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001267 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001268 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001269 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001270 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1271
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001272 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001273 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001274
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001275 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001276 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001277 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001278 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001279 if (IDVal == ".zero")
1280 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001281
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001282 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001283
Benjamin Kramere14a3c52012-05-12 11:18:59 +00001284 if (IDVal == ".extern") {
1285 EatToEndOfStatement(); // .extern is the default, ignore it.
1286 return false;
1287 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001288 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001289 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001290 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001291 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001292 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001293 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001294 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001295 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001296 if (IDVal == ".symbol_resolver")
1297 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001298 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001299 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001300 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001301 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001302 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001303 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001304 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001305 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001306 if (IDVal == ".weak_def_can_be_hidden")
1307 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001308
Hans Wennborg5cc64912011-06-18 13:51:54 +00001309 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001310 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001311 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001312 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001313
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001314 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001315 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001316 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001317 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001318 if (IDVal == ".incbin")
1319 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001320
Benjamin Kramer5cdf0ad2012-05-12 11:19:04 +00001321 if (IDVal == ".code16" || IDVal == ".code16gcc")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001322 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001323
Rafael Espindola761cb062012-06-03 23:57:14 +00001324 // Macro-like directives
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001325 if (IDVal == ".rept")
1326 return ParseDirectiveRept(IDLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001327 if (IDVal == ".irp")
1328 return ParseDirectiveIrp(IDLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00001329 if (IDVal == ".irpc")
1330 return ParseDirectiveIrpc(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001331 if (IDVal == ".endr")
Rafael Espindola761cb062012-06-03 23:57:14 +00001332 return ParseDirectiveEndr(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001333
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001334 // Look up the handler in the handler table.
1335 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1336 DirectiveMap.lookup(IDVal);
1337 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001338 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001339
Kevin Enderby9c656452009-09-10 20:51:44 +00001340
Jim Grosbach686c0182012-05-01 18:38:27 +00001341 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001342 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001343
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001344 CheckForValidSection();
1345
Chris Lattnera7f13542010-05-19 23:34:33 +00001346 // Canonicalize the opcode to lower case.
Chad Rosier8f138d12012-10-15 17:19:13 +00001347 SmallString<128> OpcodeStr;
Chris Lattnera7f13542010-05-19 23:34:33 +00001348 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
Chad Rosier8f138d12012-10-15 17:19:13 +00001349 OpcodeStr.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001350
Chad Rosier8f138d12012-10-15 17:19:13 +00001351 bool HadError = getTargetParser().ParseInstruction(OpcodeStr.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001352 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001353
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001354 // Dump the parsed representation, if requested.
1355 if (getShowParsedOperands()) {
1356 SmallString<256> Str;
1357 raw_svector_ostream OS(Str);
1358 OS << "parsed instruction: [";
1359 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1360 if (i != 0)
1361 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001362 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001363 }
1364 OS << "]";
1365
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001366 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001367 }
1368
Kevin Enderby613b7572011-11-01 22:27:22 +00001369 // If we are generating dwarf for assembly source files and the current
1370 // section is the initial text section then generate a .loc directive for
1371 // the instruction.
1372 if (!HadError && getContext().getGenDwarfForAssembly() &&
1373 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1374 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1375 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1376 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001377 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001378 StringRef());
1379 }
1380
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001381 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001382 if (!HadError) {
Chad Rosier84125ca2012-10-13 00:26:04 +00001383 unsigned ErrorInfo;
1384 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Opcode,
Chad Rosier8f138d12012-10-15 17:19:13 +00001385 ParsedOperands, Out,
1386 ErrorInfo,
Chad Rosier84125ca2012-10-13 00:26:04 +00001387 ParsingInlineAsm);
1388 }
Chris Lattner98986712010-01-14 22:21:20 +00001389
Chad Rosierb1f8c132012-10-18 15:49:34 +00001390 // Free any parsed operands. If parsing ms-style inline assembly the operands
1391 // will be freed by the ParseMSInlineAsm() function.
1392 if (!ParsingInlineAsm) {
1393 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1394 delete ParsedOperands[i];
1395 ParsedOperands.clear();
1396 }
Chris Lattner98986712010-01-14 22:21:20 +00001397
Chris Lattnercbf8a982010-09-11 16:18:25 +00001398 // Don't skip the rest of the line, the instruction parser is responsible for
1399 // that.
1400 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001401}
Chris Lattner9a023f72009-06-24 04:43:34 +00001402
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001403/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1404/// since they may not be able to be tokenized to get to the end of line token.
1405void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001406 if (!Lexer.is(AsmToken::EndOfStatement))
1407 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001408 // Eat EOL.
1409 Lex();
1410}
1411
1412/// ParseCppHashLineFilenameComment as this:
1413/// ::= # number "filename"
1414/// or just as a full line comment if it doesn't have a number and a string.
1415bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1416 Lex(); // Eat the hash token.
1417
1418 if (getLexer().isNot(AsmToken::Integer)) {
1419 // Consume the line since in cases it is not a well-formed line directive,
1420 // as if were simply a full line comment.
1421 EatToEndOfLine();
1422 return false;
1423 }
1424
1425 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001426 Lex();
1427
1428 if (getLexer().isNot(AsmToken::String)) {
1429 EatToEndOfLine();
1430 return false;
1431 }
1432
1433 StringRef Filename = getTok().getString();
1434 // Get rid of the enclosing quotes.
1435 Filename = Filename.substr(1, Filename.size()-2);
1436
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001437 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1438 CppHashLoc = L;
1439 CppHashFilename = Filename;
1440 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001441
1442 // Ignore any trailing characters, they're just comment.
1443 EatToEndOfLine();
1444 return false;
1445}
1446
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001447/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001448/// for the Filename and LineNo if any in the diagnostic.
1449void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1450 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1451 raw_ostream &OS = errs();
1452
1453 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1454 const SMLoc &DiagLoc = Diag.getLoc();
1455 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1456 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1457
1458 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1459 // before printing the message.
1460 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001461 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001462 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1463 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1464 }
1465
1466 // If we have not parsed a cpp hash line filename comment or the source
1467 // manager changed or buffer changed (like in a nested include) then just
1468 // print the normal diagnostic using its Filename and LineNo.
1469 if (!Parser->CppHashLineNumber ||
1470 &DiagSrcMgr != &Parser->SrcMgr ||
1471 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001472 if (Parser->SavedDiagHandler)
1473 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1474 else
1475 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001476 return;
1477 }
1478
1479 // Use the CppHashFilename and calculate a line number based on the
1480 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1481 // the diagnostic.
1482 const std::string Filename = Parser->CppHashFilename;
1483
1484 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1485 int CppHashLocLineNo =
1486 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1487 int LineNo = Parser->CppHashLineNumber - 1 +
1488 (DiagLocLineNo - CppHashLocLineNo);
1489
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001490 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1491 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001492 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001493 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001494
Benjamin Kramer04a04262011-10-16 10:48:29 +00001495 if (Parser->SavedDiagHandler)
1496 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1497 else
1498 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001499}
1500
Rafael Espindola799aacf2012-08-21 18:29:30 +00001501// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1502// difference being that that function accepts '@' as part of identifiers and
1503// we can't do that. AsmLexer.cpp should probably be changed to handle
1504// '@' as a special case when needed.
1505static bool isIdentifierChar(char c) {
1506 return isalnum(c) || c == '_' || c == '$' || c == '.';
1507}
1508
Rafael Espindola761cb062012-06-03 23:57:14 +00001509bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001510 const MacroParameters &Parameters,
1511 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001512 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001513 unsigned NParameters = Parameters.size();
1514 if (NParameters != 0 && NParameters != A.size())
1515 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001516
Preston Gurd7b6f2032012-09-19 20:36:12 +00001517 // A macro without parameters is handled differently on Darwin:
1518 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001519 while (!Body.empty()) {
1520 // Scan for the next substitution.
1521 std::size_t End = Body.size(), Pos = 0;
1522 for (; Pos != End; ++Pos) {
1523 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001524 if (!NParameters) {
1525 // This macro has no parameters, look for $0, $1, etc.
1526 if (Body[Pos] != '$' || Pos + 1 == End)
1527 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001528
Rafael Espindola65366442011-06-05 02:43:45 +00001529 char Next = Body[Pos + 1];
1530 if (Next == '$' || Next == 'n' || isdigit(Next))
1531 break;
1532 } else {
1533 // This macro has parameters, look for \foo, \bar, etc.
1534 if (Body[Pos] == '\\' && Pos + 1 != End)
1535 break;
1536 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001537 }
1538
1539 // Add the prefix.
1540 OS << Body.slice(0, Pos);
1541
1542 // Check if we reached the end.
1543 if (Pos == End)
1544 break;
1545
Rafael Espindola65366442011-06-05 02:43:45 +00001546 if (!NParameters) {
1547 switch (Body[Pos+1]) {
1548 // $$ => $
1549 case '$':
1550 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001551 break;
1552
Rafael Espindola65366442011-06-05 02:43:45 +00001553 // $n => number of arguments
1554 case 'n':
1555 OS << A.size();
1556 break;
1557
1558 // $[0-9] => argument
1559 default: {
1560 // Missing arguments are ignored.
1561 unsigned Index = Body[Pos+1] - '0';
1562 if (Index >= A.size())
1563 break;
1564
1565 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001566 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001567 ie = A[Index].end(); it != ie; ++it)
1568 OS << it->getString();
1569 break;
1570 }
1571 }
1572 Pos += 2;
1573 } else {
1574 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001575 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001576 ++I;
1577
1578 const char *Begin = Body.data() + Pos +1;
1579 StringRef Argument(Begin, I - (Pos +1));
1580 unsigned Index = 0;
1581 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001582 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001583 break;
1584
Preston Gurd7b6f2032012-09-19 20:36:12 +00001585 if (Index == NParameters) {
1586 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1587 Pos += 3;
1588 else {
1589 OS << '\\' << Argument;
1590 Pos = I;
1591 }
1592 } else {
1593 for (MacroArgument::const_iterator it = A[Index].begin(),
1594 ie = A[Index].end(); it != ie; ++it)
1595 if (it->getKind() == AsmToken::String)
1596 OS << it->getStringContents();
1597 else
1598 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001599
Preston Gurd7b6f2032012-09-19 20:36:12 +00001600 Pos += 1 + Argument.size();
1601 }
Rafael Espindola65366442011-06-05 02:43:45 +00001602 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001603 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001604 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001605 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001606
Rafael Espindola65366442011-06-05 02:43:45 +00001607 return false;
1608}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001609
Rafael Espindola65366442011-06-05 02:43:45 +00001610MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1611 MemoryBuffer *I)
1612 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1613{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001614}
1615
Preston Gurd7b6f2032012-09-19 20:36:12 +00001616static bool IsOperator(AsmToken::TokenKind kind)
1617{
1618 switch (kind)
1619 {
1620 default:
1621 return false;
1622 case AsmToken::Plus:
1623 case AsmToken::Minus:
1624 case AsmToken::Tilde:
1625 case AsmToken::Slash:
1626 case AsmToken::Star:
1627 case AsmToken::Dot:
1628 case AsmToken::Equal:
1629 case AsmToken::EqualEqual:
1630 case AsmToken::Pipe:
1631 case AsmToken::PipePipe:
1632 case AsmToken::Caret:
1633 case AsmToken::Amp:
1634 case AsmToken::AmpAmp:
1635 case AsmToken::Exclaim:
1636 case AsmToken::ExclaimEqual:
1637 case AsmToken::Percent:
1638 case AsmToken::Less:
1639 case AsmToken::LessEqual:
1640 case AsmToken::LessLess:
1641 case AsmToken::LessGreater:
1642 case AsmToken::Greater:
1643 case AsmToken::GreaterEqual:
1644 case AsmToken::GreaterGreater:
1645 return true;
1646 }
1647}
1648
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001649/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1650/// This is used for both default macro parameter values and the
1651/// arguments in macro invocations
Preston Gurd7b6f2032012-09-19 20:36:12 +00001652bool AsmParser::ParseMacroArgument(MacroArgument &MA,
1653 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001654 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001655 unsigned AddTokens = 0;
1656
1657 // gas accepts arguments separated by whitespace, except on Darwin
1658 if (!IsDarwin)
1659 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001660
1661 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001662 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1663 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001664 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001665 }
1666
1667 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1668 // Spaces and commas cannot be mixed to delimit parameters
1669 if (ArgumentDelimiter == AsmToken::Eof)
1670 ArgumentDelimiter = AsmToken::Comma;
1671 else if (ArgumentDelimiter != AsmToken::Comma) {
1672 Lexer.setSkipSpace(true);
1673 return TokError("expected ' ' for macro argument separator");
1674 }
1675 break;
1676 }
1677
1678 if (Lexer.is(AsmToken::Space)) {
1679 Lex(); // Eat spaces
1680
1681 // Spaces can delimit parameters, but could also be part an expression.
1682 // If the token after a space is an operator, add the token and the next
1683 // one into this argument
1684 if (ArgumentDelimiter == AsmToken::Space ||
1685 ArgumentDelimiter == AsmToken::Eof) {
1686 if (IsOperator(Lexer.getKind())) {
1687 // Check to see whether the token is used as an operator,
1688 // or part of an identifier
1689 const char *NextChar = getTok().getEndLoc().getPointer() + 1;
1690 if (*NextChar == ' ')
1691 AddTokens = 2;
1692 }
1693
1694 if (!AddTokens && ParenLevel == 0) {
1695 if (ArgumentDelimiter == AsmToken::Eof &&
1696 !IsOperator(Lexer.getKind()))
1697 ArgumentDelimiter = AsmToken::Space;
1698 break;
1699 }
1700 }
1701 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001702
1703 // HandleMacroEntry relies on not advancing the lexer here
1704 // to be able to fill in the remaining default parameter values
1705 if (Lexer.is(AsmToken::EndOfStatement))
1706 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001707
1708 // Adjust the current parentheses level.
1709 if (Lexer.is(AsmToken::LParen))
1710 ++ParenLevel;
1711 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1712 --ParenLevel;
1713
1714 // Append the token to the current argument list.
1715 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001716 if (AddTokens)
1717 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001718 Lex();
1719 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001720
1721 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001722 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001723 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001724 return false;
1725}
1726
1727// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001728bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001729 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001730 // Argument delimiter is initially unknown. It will be set by
1731 // ParseMacroArgument()
1732 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001733
1734 // Parse two kinds of macro invocations:
1735 // - macros defined without any parameters accept an arbitrary number of them
1736 // - macros defined with parameters accept at most that many of them
1737 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1738 ++Parameter) {
1739 MacroArgument MA;
1740
Preston Gurd7b6f2032012-09-19 20:36:12 +00001741 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001742 return true;
1743
Preston Gurd6c9176a2012-09-19 20:29:04 +00001744 if (!MA.empty() || !NParameters)
1745 A.push_back(MA);
1746 else if (NParameters) {
1747 if (!M->Parameters[Parameter].second.empty())
1748 A.push_back(M->Parameters[Parameter].second);
1749 }
Jim Grosbach97146442012-07-30 22:44:17 +00001750
Preston Gurd6c9176a2012-09-19 20:29:04 +00001751 // At the end of the statement, fill in remaining arguments that have
1752 // default values. If there aren't any, then the next argument is
1753 // required but missing
1754 if (Lexer.is(AsmToken::EndOfStatement)) {
1755 if (NParameters && Parameter < NParameters - 1) {
1756 if (M->Parameters[Parameter + 1].second.empty())
1757 return TokError("macro argument '" +
1758 Twine(M->Parameters[Parameter + 1].first) +
1759 "' is missing");
1760 else
1761 continue;
1762 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001763 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001764 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001765
1766 if (Lexer.is(AsmToken::Comma))
1767 Lex();
1768 }
1769 return TokError("Too many arguments");
1770}
1771
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001772bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1773 const Macro *M) {
1774 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1775 // this, although we should protect against infinite loops.
1776 if (ActiveMacros.size() == 20)
1777 return TokError("macros cannot be nested more than 20 levels deep");
1778
Rafael Espindola8a403d32012-08-08 14:51:03 +00001779 MacroArguments A;
1780 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001781 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001782
Jim Grosbach97146442012-07-30 22:44:17 +00001783 // Remove any trailing empty arguments. Do this after-the-fact as we have
1784 // to keep empty arguments in the middle of the list or positionality
1785 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001786 while (!A.empty() && A.back().empty())
1787 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001788
Rafael Espindola65366442011-06-05 02:43:45 +00001789 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1790 // to hold the macro body with substitutions.
1791 SmallString<256> Buf;
1792 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001793 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001794
Rafael Espindola8a403d32012-08-08 14:51:03 +00001795 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001796 return true;
1797
Rafael Espindola761cb062012-06-03 23:57:14 +00001798 // We include the .endmacro in the buffer as our queue to exit the macro
1799 // instantiation.
1800 OS << ".endmacro\n";
1801
Rafael Espindola65366442011-06-05 02:43:45 +00001802 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001803 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001804
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001805 // Create the macro instantiation object and add to the current macro
1806 // instantiation stack.
1807 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001808 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001809 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001810 ActiveMacros.push_back(MI);
1811
1812 // Jump to the macro instantiation and prime the lexer.
1813 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1814 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1815 Lex();
1816
1817 return false;
1818}
1819
1820void AsmParser::HandleMacroExit() {
1821 // Jump to the EndOfStatement we should return to, and consume it.
1822 JumpToLoc(ActiveMacros.back()->ExitLoc);
1823 Lex();
1824
1825 // Pop the instantiation entry.
1826 delete ActiveMacros.back();
1827 ActiveMacros.pop_back();
1828}
1829
Rafael Espindolae71cc862012-01-28 05:57:00 +00001830static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001831 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001832 case MCExpr::Binary: {
1833 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1834 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001835 break;
1836 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001837 case MCExpr::Target:
1838 case MCExpr::Constant:
1839 return false;
1840 case MCExpr::SymbolRef: {
1841 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001842 if (S.isVariable())
1843 return IsUsedIn(Sym, S.getVariableValue());
1844 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001845 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001846 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001847 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001848 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001849
1850 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001851}
1852
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001853bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1854 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001855 // FIXME: Use better location, we should use proper tokens.
1856 SMLoc EqualLoc = Lexer.getLoc();
1857
Daniel Dunbar821e3332009-08-31 08:09:28 +00001858 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001859 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001860 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001861
Rafael Espindolae71cc862012-01-28 05:57:00 +00001862 // Note: we don't count b as used in "a = b". This is to allow
1863 // a = b
1864 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001865
Daniel Dunbar3f872332009-07-28 16:08:33 +00001866 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001867 return TokError("unexpected token in assignment");
1868
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001869 // Error on assignment to '.'.
1870 if (Name == ".") {
1871 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1872 "(use '.space' or '.org').)"));
1873 }
1874
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001875 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001876 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001877
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001878 // Validate that the LHS is allowed to be a variable (either it has not been
1879 // used as a symbol, or it is an absolute symbol).
1880 MCSymbol *Sym = getContext().LookupSymbol(Name);
1881 if (Sym) {
1882 // Diagnose assignment to a label.
1883 //
1884 // FIXME: Diagnostics. Note the location of the definition as a label.
1885 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001886 if (IsUsedIn(Sym, Value))
1887 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1888 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001889 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001890 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1891 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001892 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001893 return Error(EqualLoc, "redefinition of '" + Name + "'");
1894 else if (!Sym->isVariable())
1895 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001896 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001897 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1898 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001899
1900 // Don't count these checks as uses.
1901 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001902 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001903 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001904
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001905 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001906
1907 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001908 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001909 if (NoDeadStrip)
1910 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1911
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001912
1913 return false;
1914}
1915
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001916/// ParseIdentifier:
1917/// ::= identifier
1918/// ::= string
1919bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001920 // The assembler has relaxed rules for accepting identifiers, in particular we
1921 // allow things like '.globl $foo', which would normally be separate
1922 // tokens. At this level, we have already lexed so we cannot (currently)
1923 // handle this as a context dependent token, instead we detect adjacent tokens
1924 // and return the combined identifier.
1925 if (Lexer.is(AsmToken::Dollar)) {
1926 SMLoc DollarLoc = getLexer().getLoc();
1927
1928 // Consume the dollar sign, and check for a following identifier.
1929 Lex();
1930 if (Lexer.isNot(AsmToken::Identifier))
1931 return true;
1932
1933 // We have a '$' followed by an identifier, make sure they are adjacent.
1934 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1935 return true;
1936
1937 // Construct the joined identifier and consume the token.
1938 Res = StringRef(DollarLoc.getPointer(),
1939 getTok().getIdentifier().size() + 1);
1940 Lex();
1941 return false;
1942 }
1943
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001944 if (Lexer.isNot(AsmToken::Identifier) &&
1945 Lexer.isNot(AsmToken::String))
1946 return true;
1947
Sean Callanan18b83232010-01-19 21:44:56 +00001948 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001949
Sean Callanan79ed1a82010-01-19 20:22:31 +00001950 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001951
1952 return false;
1953}
1954
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001955/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001956/// ::= .equ identifier ',' expression
1957/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001958/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001959bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001960 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001961
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001962 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001963 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001964
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001965 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001966 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001967 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001968
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001969 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001970}
1971
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001972bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001973 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001974
1975 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001976 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001977 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1978 if (Str[i] != '\\') {
1979 Data += Str[i];
1980 continue;
1981 }
1982
1983 // Recognize escaped characters. Note that this escape semantics currently
1984 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1985 ++i;
1986 if (i == e)
1987 return TokError("unexpected backslash at end of string");
1988
1989 // Recognize octal sequences.
1990 if ((unsigned) (Str[i] - '0') <= 7) {
1991 // Consume up to three octal characters.
1992 unsigned Value = Str[i] - '0';
1993
1994 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1995 ++i;
1996 Value = Value * 8 + (Str[i] - '0');
1997
1998 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1999 ++i;
2000 Value = Value * 8 + (Str[i] - '0');
2001 }
2002 }
2003
2004 if (Value > 255)
2005 return TokError("invalid octal escape sequence (out of range)");
2006
2007 Data += (unsigned char) Value;
2008 continue;
2009 }
2010
2011 // Otherwise recognize individual escapes.
2012 switch (Str[i]) {
2013 default:
2014 // Just reject invalid escape sequences for now.
2015 return TokError("invalid escape sequence (unrecognized character)");
2016
2017 case 'b': Data += '\b'; break;
2018 case 'f': Data += '\f'; break;
2019 case 'n': Data += '\n'; break;
2020 case 'r': Data += '\r'; break;
2021 case 't': Data += '\t'; break;
2022 case '"': Data += '"'; break;
2023 case '\\': Data += '\\'; break;
2024 }
2025 }
2026
2027 return false;
2028}
2029
Daniel Dunbara0d14262009-06-24 23:30:00 +00002030/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002031/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2032bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002033 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002034 CheckForValidSection();
2035
Daniel Dunbara0d14262009-06-24 23:30:00 +00002036 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002037 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002038 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002039
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002040 std::string Data;
2041 if (ParseEscapedString(Data))
2042 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002043
2044 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002045 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002046 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2047
Sean Callanan79ed1a82010-01-19 20:22:31 +00002048 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002049
2050 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002051 break;
2052
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002053 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002054 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002055 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002056 }
2057 }
2058
Sean Callanan79ed1a82010-01-19 20:22:31 +00002059 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002060 return false;
2061}
2062
2063/// ParseDirectiveValue
2064/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2065bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002066 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002067 CheckForValidSection();
2068
Daniel Dunbara0d14262009-06-24 23:30:00 +00002069 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002070 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002071 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002072 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002073 return true;
2074
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002075 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002076 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2077 assert(Size <= 8 && "Invalid size");
2078 uint64_t IntValue = MCE->getValue();
2079 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2080 return Error(ExprLoc, "literal value out of range for directive");
2081 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2082 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002083 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002084
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002085 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002086 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002087
Daniel Dunbara0d14262009-06-24 23:30:00 +00002088 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002089 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002090 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002091 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002092 }
2093 }
2094
Sean Callanan79ed1a82010-01-19 20:22:31 +00002095 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002096 return false;
2097}
2098
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002099/// ParseDirectiveRealValue
2100/// ::= (.single | .double) [ expression (, expression)* ]
2101bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2102 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2103 CheckForValidSection();
2104
2105 for (;;) {
2106 // We don't truly support arithmetic on floating point expressions, so we
2107 // have to manually parse unary prefixes.
2108 bool IsNeg = false;
2109 if (getLexer().is(AsmToken::Minus)) {
2110 Lex();
2111 IsNeg = true;
2112 } else if (getLexer().is(AsmToken::Plus))
2113 Lex();
2114
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002115 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002116 getLexer().isNot(AsmToken::Real) &&
2117 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002118 return TokError("unexpected token in directive");
2119
2120 // Convert to an APFloat.
2121 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002122 StringRef IDVal = getTok().getString();
2123 if (getLexer().is(AsmToken::Identifier)) {
2124 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2125 Value = APFloat::getInf(Semantics);
2126 else if (!IDVal.compare_lower("nan"))
2127 Value = APFloat::getNaN(Semantics, false, ~0);
2128 else
2129 return TokError("invalid floating point literal");
2130 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002131 APFloat::opInvalidOp)
2132 return TokError("invalid floating point literal");
2133 if (IsNeg)
2134 Value.changeSign();
2135
2136 // Consume the numeric token.
2137 Lex();
2138
2139 // Emit the value as an integer.
2140 APInt AsInt = Value.bitcastToAPInt();
2141 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2142 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2143
2144 if (getLexer().is(AsmToken::EndOfStatement))
2145 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002146
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002147 if (getLexer().isNot(AsmToken::Comma))
2148 return TokError("unexpected token in directive");
2149 Lex();
2150 }
2151 }
2152
2153 Lex();
2154 return false;
2155}
2156
Daniel Dunbara0d14262009-06-24 23:30:00 +00002157/// ParseDirectiveSpace
2158/// ::= .space expression [ , expression ]
2159bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002160 CheckForValidSection();
2161
Daniel Dunbara0d14262009-06-24 23:30:00 +00002162 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002163 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002164 return true;
2165
2166 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002167 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2168 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002169 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002170 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002171
Daniel Dunbar475839e2009-06-29 20:37:27 +00002172 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002173 return true;
2174
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002175 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002176 return TokError("unexpected token in '.space' directive");
2177 }
2178
Sean Callanan79ed1a82010-01-19 20:22:31 +00002179 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002180
2181 if (NumBytes <= 0)
2182 return TokError("invalid number of bytes in '.space' directive");
2183
2184 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002185 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002186
2187 return false;
2188}
2189
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002190/// ParseDirectiveZero
2191/// ::= .zero expression
2192bool AsmParser::ParseDirectiveZero() {
2193 CheckForValidSection();
2194
2195 int64_t NumBytes;
2196 if (ParseAbsoluteExpression(NumBytes))
2197 return true;
2198
Rafael Espindolae452b172010-10-05 19:42:57 +00002199 int64_t Val = 0;
2200 if (getLexer().is(AsmToken::Comma)) {
2201 Lex();
2202 if (ParseAbsoluteExpression(Val))
2203 return true;
2204 }
2205
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002206 if (getLexer().isNot(AsmToken::EndOfStatement))
2207 return TokError("unexpected token in '.zero' directive");
2208
2209 Lex();
2210
Rafael Espindolae452b172010-10-05 19:42:57 +00002211 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002212
2213 return false;
2214}
2215
Daniel Dunbara0d14262009-06-24 23:30:00 +00002216/// ParseDirectiveFill
2217/// ::= .fill expression , expression , expression
2218bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002219 CheckForValidSection();
2220
Daniel Dunbara0d14262009-06-24 23:30:00 +00002221 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002222 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002223 return true;
2224
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002225 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002226 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002227 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002228
Daniel Dunbara0d14262009-06-24 23:30:00 +00002229 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002230 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002231 return true;
2232
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002233 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002234 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002235 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002236
Daniel Dunbara0d14262009-06-24 23:30:00 +00002237 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002238 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002239 return true;
2240
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002241 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002242 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002243
Sean Callanan79ed1a82010-01-19 20:22:31 +00002244 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002245
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002246 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2247 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002248
2249 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002250 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002251
2252 return false;
2253}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002254
2255/// ParseDirectiveOrg
2256/// ::= .org expression [ , expression ]
2257bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002258 CheckForValidSection();
2259
Daniel Dunbar821e3332009-08-31 08:09:28 +00002260 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002261 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002262 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002263 return true;
2264
2265 // Parse optional fill expression.
2266 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002267 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2268 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002269 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002270 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002271
Daniel Dunbar475839e2009-06-29 20:37:27 +00002272 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002273 return true;
2274
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002275 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002276 return TokError("unexpected token in '.org' directive");
2277 }
2278
Sean Callanan79ed1a82010-01-19 20:22:31 +00002279 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002280
Jim Grosbachebd4c052012-01-27 00:37:08 +00002281 // Only limited forms of relocatable expressions are accepted here, it
2282 // has to be relative to the current section. The streamer will return
2283 // 'true' if the expression wasn't evaluatable.
2284 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2285 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002286
2287 return false;
2288}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002289
2290/// ParseDirectiveAlign
2291/// ::= {.align, ...} expression [ , expression [ , expression ]]
2292bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002293 CheckForValidSection();
2294
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002295 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002296 int64_t Alignment;
2297 if (ParseAbsoluteExpression(Alignment))
2298 return true;
2299
2300 SMLoc MaxBytesLoc;
2301 bool HasFillExpr = false;
2302 int64_t FillExpr = 0;
2303 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002304 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2305 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002306 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002307 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002308
2309 // The fill expression can be omitted while specifying a maximum number of
2310 // alignment bytes, e.g:
2311 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002312 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002313 HasFillExpr = true;
2314 if (ParseAbsoluteExpression(FillExpr))
2315 return true;
2316 }
2317
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002318 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2319 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002320 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002321 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002322
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002323 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002324 if (ParseAbsoluteExpression(MaxBytesToFill))
2325 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002326
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002327 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002328 return TokError("unexpected token in directive");
2329 }
2330 }
2331
Sean Callanan79ed1a82010-01-19 20:22:31 +00002332 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002333
Daniel Dunbar648ac512010-05-17 21:54:30 +00002334 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002335 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002336
2337 // Compute alignment in bytes.
2338 if (IsPow2) {
2339 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002340 if (Alignment >= 32) {
2341 Error(AlignmentLoc, "invalid alignment value");
2342 Alignment = 31;
2343 }
2344
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002345 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002346 }
2347
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002348 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002349 if (MaxBytesLoc.isValid()) {
2350 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002351 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2352 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002353 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002354 }
2355
2356 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002357 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2358 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002359 MaxBytesToFill = 0;
2360 }
2361 }
2362
Daniel Dunbar648ac512010-05-17 21:54:30 +00002363 // Check whether we should use optimal code alignment for this .align
2364 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002365 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002366 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2367 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002368 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002369 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002370 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002371 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2372 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002373 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002374
2375 return false;
2376}
2377
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002378/// ParseDirectiveSymbolAttribute
2379/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002380bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002381 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002382 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002383 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002384 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002385
2386 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002387 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002388
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002389 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002390
Jim Grosbach10ec6502011-09-15 17:56:49 +00002391 // Assembler local symbols don't make any sense here. Complain loudly.
2392 if (Sym->isTemporary())
2393 return Error(Loc, "non-local symbol required in directive");
2394
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002395 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002396
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002397 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002398 break;
2399
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002400 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002401 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002402 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002403 }
2404 }
2405
Sean Callanan79ed1a82010-01-19 20:22:31 +00002406 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002407 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002408}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002409
2410/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002411/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2412bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002413 CheckForValidSection();
2414
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002415 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002416 StringRef Name;
2417 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002418 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002419
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002420 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002421 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002422
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002423 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002424 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002425 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002426
2427 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002428 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002429 if (ParseAbsoluteExpression(Size))
2430 return true;
2431
2432 int64_t Pow2Alignment = 0;
2433 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002434 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002435 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002436 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002437 if (ParseAbsoluteExpression(Pow2Alignment))
2438 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002439
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002440 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2441 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002442 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2443
Chris Lattner258281d2010-01-19 06:22:22 +00002444 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002445 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2446 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002447 if (!isPowerOf2_64(Pow2Alignment))
2448 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2449 Pow2Alignment = Log2_64(Pow2Alignment);
2450 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002451 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002452
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002453 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002454 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002455
Sean Callanan79ed1a82010-01-19 20:22:31 +00002456 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002457
Chris Lattner1fc3d752009-07-09 17:25:12 +00002458 // NOTE: a size of zero for a .comm should create a undefined symbol
2459 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002460 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002461 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2462 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002463
Eric Christopherc260a3e2010-05-14 01:38:54 +00002464 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002465 // may internally end up wanting an alignment in bytes.
2466 // FIXME: Diagnose overflow.
2467 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002468 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2469 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002470
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002471 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002472 return Error(IDLoc, "invalid symbol redefinition");
2473
Chris Lattner1fc3d752009-07-09 17:25:12 +00002474 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002475 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002476 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002477 return false;
2478 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002479
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002480 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002481 return false;
2482}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002483
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002484/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002485/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002486bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002487 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002488 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002489
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002490 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002491 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002492 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002493
Sean Callanan79ed1a82010-01-19 20:22:31 +00002494 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002495
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002496 if (Str.empty())
2497 Error(Loc, ".abort detected. Assembly stopping.");
2498 else
2499 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002500 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002501
2502 return false;
2503}
Kevin Enderby71148242009-07-14 21:35:03 +00002504
Kevin Enderby1f049b22009-07-14 23:21:55 +00002505/// ParseDirectiveInclude
2506/// ::= .include "filename"
2507bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002508 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002509 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002510
Sean Callanan18b83232010-01-19 21:44:56 +00002511 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002512 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002513 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002514
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002515 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002516 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002517
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002518 // Strip the quotes.
2519 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002520
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002521 // Attempt to switch the lexer to the included file before consuming the end
2522 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002523 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002524 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002525 return true;
2526 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002527
2528 return false;
2529}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002530
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002531/// ParseDirectiveIncbin
2532/// ::= .incbin "filename"
2533bool AsmParser::ParseDirectiveIncbin() {
2534 if (getLexer().isNot(AsmToken::String))
2535 return TokError("expected string in '.incbin' directive");
2536
2537 std::string Filename = getTok().getString();
2538 SMLoc IncbinLoc = getLexer().getLoc();
2539 Lex();
2540
2541 if (getLexer().isNot(AsmToken::EndOfStatement))
2542 return TokError("unexpected token in '.incbin' directive");
2543
2544 // Strip the quotes.
2545 Filename = Filename.substr(1, Filename.size()-2);
2546
2547 // Attempt to process the included file.
2548 if (ProcessIncbinFile(Filename)) {
2549 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2550 return true;
2551 }
2552
2553 return false;
2554}
2555
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002556/// ParseDirectiveIf
2557/// ::= .if expression
2558bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002559 TheCondStack.push_back(TheCondState);
2560 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002561 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002562 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002563 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002564 int64_t ExprValue;
2565 if (ParseAbsoluteExpression(ExprValue))
2566 return true;
2567
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002568 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002569 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002570
Sean Callanan79ed1a82010-01-19 20:22:31 +00002571 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002572
2573 TheCondState.CondMet = ExprValue;
2574 TheCondState.Ignore = !TheCondState.CondMet;
2575 }
2576
2577 return false;
2578}
2579
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002580/// ParseDirectiveIfb
2581/// ::= .ifb string
2582bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2583 TheCondStack.push_back(TheCondState);
2584 TheCondState.TheCond = AsmCond::IfCond;
2585
Benjamin Kramer29739e72012-05-12 16:52:21 +00002586 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002587 EatToEndOfStatement();
2588 } else {
2589 StringRef Str = ParseStringToEndOfStatement();
2590
2591 if (getLexer().isNot(AsmToken::EndOfStatement))
2592 return TokError("unexpected token in '.ifb' directive");
2593
2594 Lex();
2595
2596 TheCondState.CondMet = ExpectBlank == Str.empty();
2597 TheCondState.Ignore = !TheCondState.CondMet;
2598 }
2599
2600 return false;
2601}
2602
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002603/// ParseDirectiveIfc
2604/// ::= .ifc string1, string2
2605bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2606 TheCondStack.push_back(TheCondState);
2607 TheCondState.TheCond = AsmCond::IfCond;
2608
Benjamin Kramer29739e72012-05-12 16:52:21 +00002609 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002610 EatToEndOfStatement();
2611 } else {
2612 StringRef Str1 = ParseStringToComma();
2613
2614 if (getLexer().isNot(AsmToken::Comma))
2615 return TokError("unexpected token in '.ifc' directive");
2616
2617 Lex();
2618
2619 StringRef Str2 = ParseStringToEndOfStatement();
2620
2621 if (getLexer().isNot(AsmToken::EndOfStatement))
2622 return TokError("unexpected token in '.ifc' directive");
2623
2624 Lex();
2625
2626 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2627 TheCondState.Ignore = !TheCondState.CondMet;
2628 }
2629
2630 return false;
2631}
2632
2633/// ParseDirectiveIfdef
2634/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002635bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2636 StringRef Name;
2637 TheCondStack.push_back(TheCondState);
2638 TheCondState.TheCond = AsmCond::IfCond;
2639
2640 if (TheCondState.Ignore) {
2641 EatToEndOfStatement();
2642 } else {
2643 if (ParseIdentifier(Name))
2644 return TokError("expected identifier after '.ifdef'");
2645
2646 Lex();
2647
2648 MCSymbol *Sym = getContext().LookupSymbol(Name);
2649
2650 if (expect_defined)
2651 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2652 else
2653 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2654 TheCondState.Ignore = !TheCondState.CondMet;
2655 }
2656
2657 return false;
2658}
2659
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002660/// ParseDirectiveElseIf
2661/// ::= .elseif expression
2662bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2663 if (TheCondState.TheCond != AsmCond::IfCond &&
2664 TheCondState.TheCond != AsmCond::ElseIfCond)
2665 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2666 " an .elseif");
2667 TheCondState.TheCond = AsmCond::ElseIfCond;
2668
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002669 bool LastIgnoreState = false;
2670 if (!TheCondStack.empty())
2671 LastIgnoreState = TheCondStack.back().Ignore;
2672 if (LastIgnoreState || TheCondState.CondMet) {
2673 TheCondState.Ignore = true;
2674 EatToEndOfStatement();
2675 }
2676 else {
2677 int64_t ExprValue;
2678 if (ParseAbsoluteExpression(ExprValue))
2679 return true;
2680
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002681 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002682 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002683
Sean Callanan79ed1a82010-01-19 20:22:31 +00002684 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002685 TheCondState.CondMet = ExprValue;
2686 TheCondState.Ignore = !TheCondState.CondMet;
2687 }
2688
2689 return false;
2690}
2691
2692/// ParseDirectiveElse
2693/// ::= .else
2694bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002695 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002696 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002697
Sean Callanan79ed1a82010-01-19 20:22:31 +00002698 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002699
2700 if (TheCondState.TheCond != AsmCond::IfCond &&
2701 TheCondState.TheCond != AsmCond::ElseIfCond)
2702 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2703 ".elseif");
2704 TheCondState.TheCond = AsmCond::ElseCond;
2705 bool LastIgnoreState = false;
2706 if (!TheCondStack.empty())
2707 LastIgnoreState = TheCondStack.back().Ignore;
2708 if (LastIgnoreState || TheCondState.CondMet)
2709 TheCondState.Ignore = true;
2710 else
2711 TheCondState.Ignore = false;
2712
2713 return false;
2714}
2715
2716/// ParseDirectiveEndIf
2717/// ::= .endif
2718bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002719 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002720 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002721
Sean Callanan79ed1a82010-01-19 20:22:31 +00002722 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002723
2724 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2725 TheCondStack.empty())
2726 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2727 ".else");
2728 if (!TheCondStack.empty()) {
2729 TheCondState = TheCondStack.back();
2730 TheCondStack.pop_back();
2731 }
2732
2733 return false;
2734}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002735
2736/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002737/// ::= .file [number] filename
2738/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002739bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002740 // FIXME: I'm not sure what this is.
2741 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002742 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002743 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002744 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002745 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002746
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002747 if (FileNumber < 1)
2748 return TokError("file number less than one");
2749 }
2750
Daniel Dunbareceec052010-07-12 17:45:27 +00002751 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002752 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002753
Nick Lewycky44d798d2011-10-17 23:05:28 +00002754 // Usually the directory and filename together, otherwise just the directory.
2755 StringRef Path = getTok().getString();
2756 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002757 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002758
Nick Lewycky44d798d2011-10-17 23:05:28 +00002759 StringRef Directory;
2760 StringRef Filename;
2761 if (getLexer().is(AsmToken::String)) {
2762 if (FileNumber == -1)
2763 return TokError("explicit path specified, but no file number");
2764 Filename = getTok().getString();
2765 Filename = Filename.substr(1, Filename.size()-2);
2766 Directory = Path;
2767 Lex();
2768 } else {
2769 Filename = Path;
2770 }
2771
Daniel Dunbareceec052010-07-12 17:45:27 +00002772 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002773 return TokError("unexpected token in '.file' directive");
2774
Chris Lattnerd32e8032010-01-25 19:02:58 +00002775 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002776 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002777 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002778 if (getContext().getGenDwarfForAssembly() == true)
2779 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2780 "used to generate dwarf debug info for assembly code");
2781
Nick Lewycky44d798d2011-10-17 23:05:28 +00002782 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002783 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002784 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002785
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002786 return false;
2787}
2788
2789/// ParseDirectiveLine
2790/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002791bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002792 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2793 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002794 return TokError("unexpected token in '.line' directive");
2795
Sean Callanan18b83232010-01-19 21:44:56 +00002796 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002797 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002798 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002799
2800 // FIXME: Do something with the .line.
2801 }
2802
Daniel Dunbareceec052010-07-12 17:45:27 +00002803 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002804 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002805
2806 return false;
2807}
2808
2809
2810/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002811/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002812/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2813/// The first number is a file number, must have been previously assigned with
2814/// a .file directive, the second number is the line number and optionally the
2815/// third number is a column position (zero if not specified). The remaining
2816/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002817bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002818
Daniel Dunbareceec052010-07-12 17:45:27 +00002819 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002820 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002821 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002822 if (FileNumber < 1)
2823 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002824 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002825 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002826 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002827
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002828 int64_t LineNumber = 0;
2829 if (getLexer().is(AsmToken::Integer)) {
2830 LineNumber = getTok().getIntVal();
2831 if (LineNumber < 1)
2832 return TokError("line number less than one in '.loc' directive");
2833 Lex();
2834 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002835
2836 int64_t ColumnPos = 0;
2837 if (getLexer().is(AsmToken::Integer)) {
2838 ColumnPos = getTok().getIntVal();
2839 if (ColumnPos < 0)
2840 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002841 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002842 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002843
Kevin Enderbyc0957932010-09-30 16:52:03 +00002844 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002845 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002846 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002847 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2848 for (;;) {
2849 if (getLexer().is(AsmToken::EndOfStatement))
2850 break;
2851
2852 StringRef Name;
2853 SMLoc Loc = getTok().getLoc();
2854 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002855 return TokError("unexpected token in '.loc' directive");
2856
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002857 if (Name == "basic_block")
2858 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2859 else if (Name == "prologue_end")
2860 Flags |= DWARF2_FLAG_PROLOGUE_END;
2861 else if (Name == "epilogue_begin")
2862 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2863 else if (Name == "is_stmt") {
2864 SMLoc Loc = getTok().getLoc();
2865 const MCExpr *Value;
2866 if (getParser().ParseExpression(Value))
2867 return true;
2868 // The expression must be the constant 0 or 1.
2869 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2870 int Value = MCE->getValue();
2871 if (Value == 0)
2872 Flags &= ~DWARF2_FLAG_IS_STMT;
2873 else if (Value == 1)
2874 Flags |= DWARF2_FLAG_IS_STMT;
2875 else
2876 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002877 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002878 else {
2879 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2880 }
2881 }
2882 else if (Name == "isa") {
2883 SMLoc Loc = getTok().getLoc();
2884 const MCExpr *Value;
2885 if (getParser().ParseExpression(Value))
2886 return true;
2887 // The expression must be a constant greater or equal to 0.
2888 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2889 int Value = MCE->getValue();
2890 if (Value < 0)
2891 return Error(Loc, "isa number less than zero");
2892 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002893 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002894 else {
2895 return Error(Loc, "isa number not a constant value");
2896 }
2897 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002898 else if (Name == "discriminator") {
2899 if (getParser().ParseAbsoluteExpression(Discriminator))
2900 return true;
2901 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002902 else {
2903 return Error(Loc, "unknown sub-directive in '.loc' directive");
2904 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002905
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002906 if (getLexer().is(AsmToken::EndOfStatement))
2907 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002908 }
2909 }
2910
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002911 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002912 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002913
2914 return false;
2915}
2916
Daniel Dunbar138abae2010-10-16 04:56:42 +00002917/// ParseDirectiveStabs
2918/// ::= .stabs string, number, number, number
2919bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2920 SMLoc DirectiveLoc) {
2921 return TokError("unsupported directive '" + Directive + "'");
2922}
2923
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002924/// ParseDirectiveCFISections
2925/// ::= .cfi_sections section [, section]
2926bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2927 SMLoc DirectiveLoc) {
2928 StringRef Name;
2929 bool EH = false;
2930 bool Debug = false;
2931
2932 if (getParser().ParseIdentifier(Name))
2933 return TokError("Expected an identifier");
2934
2935 if (Name == ".eh_frame")
2936 EH = true;
2937 else if (Name == ".debug_frame")
2938 Debug = true;
2939
2940 if (getLexer().is(AsmToken::Comma)) {
2941 Lex();
2942
2943 if (getParser().ParseIdentifier(Name))
2944 return TokError("Expected an identifier");
2945
2946 if (Name == ".eh_frame")
2947 EH = true;
2948 else if (Name == ".debug_frame")
2949 Debug = true;
2950 }
2951
2952 getStreamer().EmitCFISections(EH, Debug);
2953
2954 return false;
2955}
2956
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002957/// ParseDirectiveCFIStartProc
2958/// ::= .cfi_startproc
2959bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2960 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002961 getStreamer().EmitCFIStartProc();
2962 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002963}
2964
2965/// ParseDirectiveCFIEndProc
2966/// ::= .cfi_endproc
2967bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002968 getStreamer().EmitCFIEndProc();
2969 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002970}
2971
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002972/// ParseRegisterOrRegisterNumber - parse register name or number.
2973bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2974 SMLoc DirectiveLoc) {
2975 unsigned RegNo;
2976
Jim Grosbach6f888a82011-06-02 17:14:04 +00002977 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002978 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2979 DirectiveLoc))
2980 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002981 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002982 } else
2983 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002984
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002985 return false;
2986}
2987
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002988/// ParseDirectiveCFIDefCfa
2989/// ::= .cfi_def_cfa register, offset
2990bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2991 SMLoc DirectiveLoc) {
2992 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002993 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002994 return true;
2995
2996 if (getLexer().isNot(AsmToken::Comma))
2997 return TokError("unexpected token in directive");
2998 Lex();
2999
3000 int64_t Offset = 0;
3001 if (getParser().ParseAbsoluteExpression(Offset))
3002 return true;
3003
Rafael Espindola066c2f42011-04-12 23:59:07 +00003004 getStreamer().EmitCFIDefCfa(Register, Offset);
3005 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003006}
3007
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003008/// ParseDirectiveCFIDefCfaOffset
3009/// ::= .cfi_def_cfa_offset offset
3010bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
3011 SMLoc DirectiveLoc) {
3012 int64_t Offset = 0;
3013 if (getParser().ParseAbsoluteExpression(Offset))
3014 return true;
3015
Rafael Espindola066c2f42011-04-12 23:59:07 +00003016 getStreamer().EmitCFIDefCfaOffset(Offset);
3017 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00003018}
3019
3020/// ParseDirectiveCFIAdjustCfaOffset
3021/// ::= .cfi_adjust_cfa_offset adjustment
3022bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
3023 SMLoc DirectiveLoc) {
3024 int64_t Adjustment = 0;
3025 if (getParser().ParseAbsoluteExpression(Adjustment))
3026 return true;
3027
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00003028 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3029 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003030}
3031
3032/// ParseDirectiveCFIDefCfaRegister
3033/// ::= .cfi_def_cfa_register register
3034bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
3035 SMLoc DirectiveLoc) {
3036 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003037 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003038 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003039
Rafael Espindola066c2f42011-04-12 23:59:07 +00003040 getStreamer().EmitCFIDefCfaRegister(Register);
3041 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003042}
3043
3044/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003045/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003046bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3047 int64_t Register = 0;
3048 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003049
3050 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003051 return true;
3052
3053 if (getLexer().isNot(AsmToken::Comma))
3054 return TokError("unexpected token in directive");
3055 Lex();
3056
3057 if (getParser().ParseAbsoluteExpression(Offset))
3058 return true;
3059
Rafael Espindola066c2f42011-04-12 23:59:07 +00003060 getStreamer().EmitCFIOffset(Register, Offset);
3061 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003062}
3063
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003064/// ParseDirectiveCFIRelOffset
3065/// ::= .cfi_rel_offset register, offset
3066bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3067 SMLoc DirectiveLoc) {
3068 int64_t Register = 0;
3069
3070 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3071 return true;
3072
3073 if (getLexer().isNot(AsmToken::Comma))
3074 return TokError("unexpected token in directive");
3075 Lex();
3076
3077 int64_t Offset = 0;
3078 if (getParser().ParseAbsoluteExpression(Offset))
3079 return true;
3080
Rafael Espindola25f492e2011-04-12 16:12:03 +00003081 getStreamer().EmitCFIRelOffset(Register, Offset);
3082 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003083}
3084
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003085static bool isValidEncoding(int64_t Encoding) {
3086 if (Encoding & ~0xff)
3087 return false;
3088
3089 if (Encoding == dwarf::DW_EH_PE_omit)
3090 return true;
3091
3092 const unsigned Format = Encoding & 0xf;
3093 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3094 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3095 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3096 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3097 return false;
3098
Rafael Espindolacaf11582010-12-29 04:31:26 +00003099 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003100 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003101 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003102 return false;
3103
3104 return true;
3105}
3106
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003107/// ParseDirectiveCFIPersonalityOrLsda
3108/// ::= .cfi_personality encoding, [symbol_name]
3109/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003110bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003111 SMLoc DirectiveLoc) {
3112 int64_t Encoding = 0;
3113 if (getParser().ParseAbsoluteExpression(Encoding))
3114 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003115 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003116 return false;
3117
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003118 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003119 return TokError("unsupported encoding.");
3120
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003121 if (getLexer().isNot(AsmToken::Comma))
3122 return TokError("unexpected token in directive");
3123 Lex();
3124
3125 StringRef Name;
3126 if (getParser().ParseIdentifier(Name))
3127 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003128
3129 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3130
3131 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003132 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003133 else {
3134 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003135 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003136 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003137 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003138}
3139
Rafael Espindolafe024d02010-12-28 18:36:23 +00003140/// ParseDirectiveCFIRememberState
3141/// ::= .cfi_remember_state
3142bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3143 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003144 getStreamer().EmitCFIRememberState();
3145 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003146}
3147
3148/// ParseDirectiveCFIRestoreState
3149/// ::= .cfi_remember_state
3150bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3151 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003152 getStreamer().EmitCFIRestoreState();
3153 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003154}
3155
Rafael Espindolac5754392011-04-12 15:31:05 +00003156/// ParseDirectiveCFISameValue
3157/// ::= .cfi_same_value register
3158bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3159 SMLoc DirectiveLoc) {
3160 int64_t Register = 0;
3161
3162 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3163 return true;
3164
3165 getStreamer().EmitCFISameValue(Register);
3166
3167 return false;
3168}
3169
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003170/// ParseDirectiveCFIRestore
3171/// ::= .cfi_restore register
3172bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003173 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003174 int64_t Register = 0;
3175 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3176 return true;
3177
3178 getStreamer().EmitCFIRestore(Register);
3179
3180 return false;
3181}
3182
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003183/// ParseDirectiveCFIEscape
3184/// ::= .cfi_escape expression[,...]
3185bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003186 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003187 std::string Values;
3188 int64_t CurrValue;
3189 if (getParser().ParseAbsoluteExpression(CurrValue))
3190 return true;
3191
3192 Values.push_back((uint8_t)CurrValue);
3193
3194 while (getLexer().is(AsmToken::Comma)) {
3195 Lex();
3196
3197 if (getParser().ParseAbsoluteExpression(CurrValue))
3198 return true;
3199
3200 Values.push_back((uint8_t)CurrValue);
3201 }
3202
3203 getStreamer().EmitCFIEscape(Values);
3204 return false;
3205}
3206
Rafael Espindola16d7d432012-01-23 21:51:52 +00003207/// ParseDirectiveCFISignalFrame
3208/// ::= .cfi_signal_frame
3209bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3210 SMLoc DirectiveLoc) {
3211 if (getLexer().isNot(AsmToken::EndOfStatement))
3212 return Error(getLexer().getLoc(),
3213 "unexpected token in '" + Directive + "' directive");
3214
3215 getStreamer().EmitCFISignalFrame();
3216
3217 return false;
3218}
3219
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003220/// ParseDirectiveMacrosOnOff
3221/// ::= .macros_on
3222/// ::= .macros_off
3223bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3224 SMLoc DirectiveLoc) {
3225 if (getLexer().isNot(AsmToken::EndOfStatement))
3226 return Error(getLexer().getLoc(),
3227 "unexpected token in '" + Directive + "' directive");
3228
3229 getParser().MacrosEnabled = Directive == ".macros_on";
3230
3231 return false;
3232}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003233
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003234/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003235/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003236bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3237 SMLoc DirectiveLoc) {
3238 StringRef Name;
3239 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003240 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003241
Rafael Espindola8a403d32012-08-08 14:51:03 +00003242 MacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003243 // Argument delimiter is initially unknown. It will be set by
3244 // ParseMacroArgument()
3245 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003246 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003247 for (;;) {
3248 MacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003249 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003250 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003251
3252 if (getLexer().is(AsmToken::Equal)) {
3253 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003254 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003255 return true;
3256 }
3257
Rafael Espindola65366442011-06-05 02:43:45 +00003258 Parameters.push_back(Parameter);
3259
Preston Gurd7b6f2032012-09-19 20:36:12 +00003260 if (getLexer().is(AsmToken::Comma))
3261 Lex();
3262 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003263 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003264 }
3265 }
3266
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003267 // Eat the end of statement.
3268 Lex();
3269
3270 AsmToken EndToken, StartToken = getTok();
3271
3272 // Lex the macro definition.
3273 for (;;) {
3274 // Check whether we have reached the end of the file.
3275 if (getLexer().is(AsmToken::Eof))
3276 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3277
3278 // Otherwise, check whether we have reach the .endmacro.
3279 if (getLexer().is(AsmToken::Identifier) &&
3280 (getTok().getIdentifier() == ".endm" ||
3281 getTok().getIdentifier() == ".endmacro")) {
3282 EndToken = getTok();
3283 Lex();
3284 if (getLexer().isNot(AsmToken::EndOfStatement))
3285 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3286 "' directive");
3287 break;
3288 }
3289
3290 // Otherwise, scan til the end of the statement.
3291 getParser().EatToEndOfStatement();
3292 }
3293
3294 if (getParser().MacroMap.lookup(Name)) {
3295 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3296 }
3297
3298 const char *BodyStart = StartToken.getLoc().getPointer();
3299 const char *BodyEnd = EndToken.getLoc().getPointer();
3300 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003301 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003302 return false;
3303}
3304
3305/// ParseDirectiveEndMacro
3306/// ::= .endm
3307/// ::= .endmacro
3308bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003309 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003310 if (getLexer().isNot(AsmToken::EndOfStatement))
3311 return TokError("unexpected token in '" + Directive + "' directive");
3312
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003313 // If we are inside a macro instantiation, terminate the current
3314 // instantiation.
3315 if (!getParser().ActiveMacros.empty()) {
3316 getParser().HandleMacroExit();
3317 return false;
3318 }
3319
3320 // Otherwise, this .endmacro is a stray entry in the file; well formed
3321 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003322 return TokError("unexpected '" + Directive + "' in file, "
3323 "no current macro definition");
3324}
3325
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003326/// ParseDirectivePurgeMacro
3327/// ::= .purgem
3328bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3329 SMLoc DirectiveLoc) {
3330 StringRef Name;
3331 if (getParser().ParseIdentifier(Name))
3332 return TokError("expected identifier in '.purgem' directive");
3333
3334 if (getLexer().isNot(AsmToken::EndOfStatement))
3335 return TokError("unexpected token in '.purgem' directive");
3336
3337 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3338 if (I == getParser().MacroMap.end())
3339 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3340
3341 // Undefine the macro.
3342 delete I->getValue();
3343 getParser().MacroMap.erase(I);
3344 return false;
3345}
3346
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003347bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003348 getParser().CheckForValidSection();
3349
3350 const MCExpr *Value;
3351
3352 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003353 return true;
3354
3355 if (getLexer().isNot(AsmToken::EndOfStatement))
3356 return TokError("unexpected token in directive");
3357
3358 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003359 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003360 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003361 getStreamer().EmitULEB128Value(Value);
3362
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003363 return false;
3364}
3365
Rafael Espindola761cb062012-06-03 23:57:14 +00003366Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003367 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003368
Rafael Espindola761cb062012-06-03 23:57:14 +00003369 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003370 for (;;) {
3371 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003372 if (getLexer().is(AsmToken::Eof)) {
3373 Error(DirectiveLoc, "no matching '.endr' in definition");
3374 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003375 }
3376
Rafael Espindola761cb062012-06-03 23:57:14 +00003377 if (Lexer.is(AsmToken::Identifier) &&
3378 (getTok().getIdentifier() == ".rept")) {
3379 ++NestLevel;
3380 }
3381
3382 // Otherwise, check whether we have reached the .endr.
3383 if (Lexer.is(AsmToken::Identifier) &&
3384 getTok().getIdentifier() == ".endr") {
3385 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003386 EndToken = getTok();
3387 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003388 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3389 TokError("unexpected token in '.endr' directive");
3390 return 0;
3391 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003392 break;
3393 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003394 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003395 }
3396
Rafael Espindola761cb062012-06-03 23:57:14 +00003397 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003398 EatToEndOfStatement();
3399 }
3400
3401 const char *BodyStart = StartToken.getLoc().getPointer();
3402 const char *BodyEnd = EndToken.getLoc().getPointer();
3403 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3404
Rafael Espindola761cb062012-06-03 23:57:14 +00003405 // We Are Anonymous.
3406 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003407 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003408 return new Macro(Name, Body, Parameters);
3409}
3410
3411void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3412 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003413 OS << ".endr\n";
3414
3415 MemoryBuffer *Instantiation =
3416 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3417
Rafael Espindola761cb062012-06-03 23:57:14 +00003418 // Create the macro instantiation object and add to the current macro
3419 // instantiation stack.
3420 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3421 getTok().getLoc(),
3422 Instantiation);
3423 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003424
Rafael Espindola761cb062012-06-03 23:57:14 +00003425 // Jump to the macro instantiation and prime the lexer.
3426 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3427 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3428 Lex();
3429}
3430
3431bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3432 int64_t Count;
3433 if (ParseAbsoluteExpression(Count))
3434 return TokError("unexpected token in '.rept' directive");
3435
3436 if (Count < 0)
3437 return TokError("Count is negative");
3438
3439 if (Lexer.isNot(AsmToken::EndOfStatement))
3440 return TokError("unexpected token in '.rept' directive");
3441
3442 // Eat the end of statement.
3443 Lex();
3444
3445 // Lex the rept definition.
3446 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3447 if (!M)
3448 return true;
3449
3450 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3451 // to hold the macro body with substitutions.
3452 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003453 MacroParameters Parameters;
3454 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003455 raw_svector_ostream OS(Buf);
3456 while (Count--) {
3457 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3458 return true;
3459 }
3460 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003461
3462 return false;
3463}
3464
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003465/// ParseDirectiveIrp
3466/// ::= .irp symbol,values
3467bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003468 MacroParameters Parameters;
3469 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003470
Preston Gurd6c9176a2012-09-19 20:29:04 +00003471 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003472 return TokError("expected identifier in '.irp' directive");
3473
3474 Parameters.push_back(Parameter);
3475
3476 if (Lexer.isNot(AsmToken::Comma))
3477 return TokError("expected comma in '.irp' directive");
3478
3479 Lex();
3480
Rafael Espindola8a403d32012-08-08 14:51:03 +00003481 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003482 if (ParseMacroArguments(0, A))
3483 return true;
3484
3485 // Eat the end of statement.
3486 Lex();
3487
3488 // Lex the irp definition.
3489 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3490 if (!M)
3491 return true;
3492
3493 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3494 // to hold the macro body with substitutions.
3495 SmallString<256> Buf;
3496 raw_svector_ostream OS(Buf);
3497
Rafael Espindola7996d042012-08-21 16:06:48 +00003498 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3499 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003500 Args.push_back(*i);
3501
3502 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3503 return true;
3504 }
3505
3506 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3507
3508 return false;
3509}
3510
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003511/// ParseDirectiveIrpc
3512/// ::= .irpc symbol,values
3513bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003514 MacroParameters Parameters;
3515 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003516
Preston Gurd6c9176a2012-09-19 20:29:04 +00003517 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003518 return TokError("expected identifier in '.irpc' directive");
3519
3520 Parameters.push_back(Parameter);
3521
3522 if (Lexer.isNot(AsmToken::Comma))
3523 return TokError("expected comma in '.irpc' directive");
3524
3525 Lex();
3526
Rafael Espindola8a403d32012-08-08 14:51:03 +00003527 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003528 if (ParseMacroArguments(0, A))
3529 return true;
3530
3531 if (A.size() != 1 || A.front().size() != 1)
3532 return TokError("unexpected token in '.irpc' directive");
3533
3534 // Eat the end of statement.
3535 Lex();
3536
3537 // Lex the irpc definition.
3538 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3539 if (!M)
3540 return true;
3541
3542 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3543 // to hold the macro body with substitutions.
3544 SmallString<256> Buf;
3545 raw_svector_ostream OS(Buf);
3546
3547 StringRef Values = A.front().front().getString();
3548 std::size_t I, End = Values.size();
3549 for (I = 0; I < End; ++I) {
3550 MacroArgument Arg;
3551 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3552
Rafael Espindola8a403d32012-08-08 14:51:03 +00003553 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003554 Args.push_back(Arg);
3555
3556 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3557 return true;
3558 }
3559
3560 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3561
3562 return false;
3563}
3564
Rafael Espindola761cb062012-06-03 23:57:14 +00003565bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3566 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003567 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003568
3569 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003570 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003571 assert(getLexer().is(AsmToken::EndOfStatement));
3572
Rafael Espindola761cb062012-06-03 23:57:14 +00003573 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003574 return false;
3575}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003576
Chad Rosierb1f8c132012-10-18 15:49:34 +00003577namespace {
3578enum AsmOpRewriteKind {
3579 AOK_Imm,
3580 AOK_Input,
Chad Rosier96d58e62012-10-19 20:57:14 +00003581 AOK_Output,
3582 AOK_SizeDirective
Chad Rosierb1f8c132012-10-18 15:49:34 +00003583};
3584
3585struct AsmOpRewrite {
3586 AsmOpRewriteKind Kind;
3587 SMLoc Loc;
3588 unsigned Len;
Chad Rosier96d58e62012-10-19 20:57:14 +00003589 unsigned Size;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003590public:
Chad Rosier96d58e62012-10-19 20:57:14 +00003591 AsmOpRewrite(AsmOpRewriteKind kind, SMLoc loc, unsigned len, unsigned size = 0)
3592 : Kind(kind), Loc(loc), Len(len), Size(size) { }
Chad Rosierb1f8c132012-10-18 15:49:34 +00003593};
3594}
3595
3596bool AsmParser::ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
3597 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003598 SmallVectorImpl<void *> &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003599 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003600 SmallVectorImpl<std::string> &Clobbers,
3601 const MCInstrInfo *MII,
3602 const MCInstPrinter *IP,
3603 MCAsmParserSemaCallback &SI) {
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003604 SmallVector<void*, 4> InputDecls;
3605 SmallVector<void*, 4> OutputDecls;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003606 SmallVector<std::string, 4> InputConstraints;
3607 SmallVector<std::string, 4> OutputConstraints;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003608 std::set<std::string> ClobberRegs;
3609
3610 SmallVector<struct AsmOpRewrite, 4> AsmStrRewrites;
3611
3612 // Prime the lexer.
3613 Lex();
3614
3615 // While we have input, parse each statement.
3616 unsigned InputIdx = 0;
3617 unsigned OutputIdx = 0;
3618 while (getLexer().isNot(AsmToken::Eof)) {
Chad Rosierab450e42012-10-19 22:57:33 +00003619 // Clear the opcode.
3620 setOpcode(~0x0);
3621
3622 if (ParseStatement())
3623 return true;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003624
3625 if (isInstruction()) {
3626 const MCInstrDesc &Desc = MII->get(getOpcode());
3627
3628 // Build the list of clobbers, outputs and inputs.
3629 for (unsigned i = 1, e = ParsedOperands.size(); i != e; ++i) {
3630 MCParsedAsmOperand *Operand = ParsedOperands[i];
3631
3632 // Immediate.
3633 if (Operand->isImm()) {
3634 AsmStrRewrites.push_back(AsmOpRewrite(AOK_Imm,
3635 Operand->getStartLoc(),
3636 Operand->getNameLen()));
3637 continue;
3638 }
3639
3640 // Register operand.
3641 if (Operand->isReg()) {
3642 unsigned NumDefs = Desc.getNumDefs();
3643 // Clobber.
3644 if (NumDefs && Operand->getMCOperandNum() < NumDefs) {
3645 std::string Reg;
3646 raw_string_ostream OS(Reg);
3647 IP->printRegName(OS, Operand->getReg());
3648 ClobberRegs.insert(StringRef(OS.str()));
3649 }
3650 continue;
3651 }
3652
3653 // Expr/Input or Output.
Chad Rosier32989592012-10-18 20:27:15 +00003654 unsigned Size;
3655 void *OpDecl = SI.LookupInlineAsmIdentifier(Operand->getName(), AsmLoc,
3656 Size);
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003657 if (OpDecl) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003658 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosier96d58e62012-10-19 20:57:14 +00003659 if (Operand->needSizeDirective())
3660 AsmStrRewrites.push_back(AsmOpRewrite(AOK_SizeDirective,
3661 Operand->getStartLoc(), 0,
3662 Operand->getMemSize()));
3663
Chad Rosierb1f8c132012-10-18 15:49:34 +00003664 if (isOutput) {
3665 std::string Constraint = "=";
3666 ++InputIdx;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003667 OutputDecls.push_back(OpDecl);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003668 Constraint += Operand->getConstraint().str();
3669 OutputConstraints.push_back(Constraint);
3670 AsmStrRewrites.push_back(AsmOpRewrite(AOK_Output,
3671 Operand->getStartLoc(),
3672 Operand->getNameLen()));
3673 } else {
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003674 InputDecls.push_back(OpDecl);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003675 InputConstraints.push_back(Operand->getConstraint().str());
3676 AsmStrRewrites.push_back(AsmOpRewrite(AOK_Input,
3677 Operand->getStartLoc(),
3678 Operand->getNameLen()));
3679 }
3680 }
3681 }
3682 // Free any parsed operands.
3683 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
3684 delete ParsedOperands[i];
3685 ParsedOperands.clear();
3686 }
3687 }
3688
3689 // Set the number of Outputs and Inputs.
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003690 NumOutputs = OutputDecls.size();
3691 NumInputs = InputDecls.size();
Chad Rosierb1f8c132012-10-18 15:49:34 +00003692
3693 // Set the unique clobbers.
3694 for (std::set<std::string>::iterator I = ClobberRegs.begin(),
3695 E = ClobberRegs.end(); I != E; ++I)
3696 Clobbers.push_back(*I);
3697
3698 // Merge the various outputs and inputs. Output are expected first.
3699 if (NumOutputs || NumInputs) {
3700 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003701 OpDecls.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003702 Constraints.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003703 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003704 OpDecls[i] = OutputDecls[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003705 Constraints[i] = OutputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003706 }
3707 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003708 OpDecls[j] = InputDecls[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003709 Constraints[j] = InputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003710 }
3711 }
3712
3713 // Build the IR assembly string.
3714 std::string AsmStringIR;
Chad Rosier96d58e62012-10-19 20:57:14 +00003715 AsmOpRewriteKind PrevKind = AOK_Imm;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003716 raw_string_ostream OS(AsmStringIR);
3717 const char *Start = SrcMgr.getMemoryBuffer(0)->getBufferStart();
3718 for (SmallVectorImpl<struct AsmOpRewrite>::iterator
3719 I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
3720 const char *Loc = (*I).Loc.getPointer();
Chad Rosier96d58e62012-10-19 20:57:14 +00003721
3722 AsmOpRewriteKind Kind = (*I).Kind;
3723
3724 // Emit everything up to the immediate/expression. If the previous rewrite
3725 // was a size directive, then this has already been done.
3726 if (PrevKind != AOK_SizeDirective)
3727 OS << StringRef(Start, Loc - Start);
3728 PrevKind = Kind;
3729
Chad Rosierb1f8c132012-10-18 15:49:34 +00003730 // Rewrite expressions in $N notation.
Chad Rosier96d58e62012-10-19 20:57:14 +00003731 switch (Kind) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003732 case AOK_Imm:
3733 OS << Twine("$$") + StringRef(Loc, (*I).Len);
3734 break;
3735 case AOK_Input:
3736 OS << '$';
3737 OS << InputIdx++;
3738 break;
3739 case AOK_Output:
3740 OS << '$';
3741 OS << OutputIdx++;
3742 break;
Chad Rosier96d58e62012-10-19 20:57:14 +00003743 case AOK_SizeDirective:
3744 switch((*I).Size) {
3745 default: break;
3746 case 8: OS << "byte ptr "; break;
3747 case 16: OS << "word ptr "; break;
3748 case 32: OS << "dword ptr "; break;
3749 case 64: OS << "qword ptr "; break;
3750 case 80: OS << "xword ptr "; break;
3751 case 128: OS << "xmmword ptr "; break;
3752 case 256: OS << "ymmword ptr "; break;
3753 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00003754 }
Chad Rosier96d58e62012-10-19 20:57:14 +00003755
Chad Rosierb1f8c132012-10-18 15:49:34 +00003756 // Skip the original expression.
Chad Rosier96d58e62012-10-19 20:57:14 +00003757 if (Kind != AOK_SizeDirective)
3758 Start = Loc + (*I).Len;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003759 }
3760
3761 // Emit the remainder of the asm string.
3762 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
3763 if (Start != AsmEnd)
3764 OS << StringRef(Start, AsmEnd - Start);
3765
3766 AsmString = OS.str();
3767 return false;
3768}
3769
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003770/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003771MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003772 MCContext &C, MCStreamer &Out,
3773 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003774 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003775}