blob: 7fad30164ee4a8c71ecc4d5f76a1b0bb62545af1 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000023#include "llvm/MC/MCParser/AsmCond.h"
24#include "llvm/MC/MCParser/AsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000027#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000028#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000029#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000030#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000031#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000032#include "llvm/Support/CommandLine.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000033#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000034#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000037#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000038#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000039using namespace llvm;
40
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000041static cl::opt<bool>
42FatalAssemblerWarnings("fatal-assembler-warnings",
43 cl::desc("Consider warnings as error"));
44
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000045namespace {
46
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000047/// \brief Helper class for tracking macro definitions.
48struct Macro {
49 StringRef Name;
50 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000051 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000052
53public:
Rafael Espindola65366442011-06-05 02:43:45 +000054 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
55 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000056};
57
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000058/// \brief Helper class for storing information about an active macro
59/// instantiation.
60struct MacroInstantiation {
61 /// The macro being instantiated.
62 const Macro *TheMacro;
63
64 /// The macro instantiation with substitutions.
65 MemoryBuffer *Instantiation;
66
67 /// The location of the instantiation.
68 SMLoc InstantiationLoc;
69
70 /// The location where parsing should resume upon instantiation completion.
71 SMLoc ExitLoc;
72
73public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000074 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000075 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000076};
77
Daniel Dunbaraef87e32010-07-18 18:31:38 +000078/// \brief The concrete assembly parser instance.
79class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000080 friend class GenericAsmParser;
81
Daniel Dunbaraef87e32010-07-18 18:31:38 +000082 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
83 void operator=(const AsmParser &); // DO NOT IMPLEMENT
84private:
85 AsmLexer Lexer;
86 MCContext &Ctx;
87 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000088 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000090 SourceMgr::DiagHandlerTy SavedDiagHandler;
91 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000092 MCAsmParserExtension *GenericParser;
93 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000094
Daniel Dunbaraef87e32010-07-18 18:31:38 +000095 /// This is the current buffer index we're lexing from as managed by the
96 /// SourceMgr object.
97 int CurBuffer;
98
99 AsmCond TheCondState;
100 std::vector<AsmCond> TheCondStack;
101
102 /// DirectiveMap - This is a table handlers for directives. Each handler is
103 /// invoked after the directive identifier is read and is responsible for
104 /// parsing and validating the rest of the directive. The handler is passed
105 /// in the directive name and the location of the directive keyword.
106 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000107
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000108 /// MacroMap - Map of currently defined macros.
109 StringMap<Macro*> MacroMap;
110
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000111 /// ActiveMacros - Stack of active macro instantiations.
112 std::vector<MacroInstantiation*> ActiveMacros;
113
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000114 /// Boolean tracking whether macro substitution is enabled.
115 unsigned MacrosEnabled : 1;
116
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000117 /// Flag tracking whether any errors have been encountered.
118 unsigned HadError : 1;
119
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000120 /// The values from the last parsed cpp hash file line comment if any.
121 StringRef CppHashFilename;
122 int64_t CppHashLineNumber;
123 SMLoc CppHashLoc;
124
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000125public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000126 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000127 const MCAsmInfo &MAI);
128 ~AsmParser();
129
130 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
131
132 void AddDirectiveHandler(MCAsmParserExtension *Object,
133 StringRef Directive,
134 DirectiveHandler Handler) {
135 DirectiveMap[Directive] = std::make_pair(Object, Handler);
136 }
137
138public:
139 /// @name MCAsmParser Interface
140 /// {
141
142 virtual SourceMgr &getSourceManager() { return SrcMgr; }
143 virtual MCAsmLexer &getLexer() { return Lexer; }
144 virtual MCContext &getContext() { return Ctx; }
145 virtual MCStreamer &getStreamer() { return Out; }
Devang Patela005c312012-01-10 21:49:42 +0000146 virtual unsigned getAssemblerDialect() { return MAI.getAssemblerDialect(); }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000147
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000148 virtual bool Warning(SMLoc L, const Twine &Msg,
149 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
150 virtual bool Error(SMLoc L, const Twine &Msg,
151 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000152
153 const AsmToken &Lex();
154
155 bool ParseExpression(const MCExpr *&Res);
156 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
157 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
158 virtual bool ParseAbsoluteExpression(int64_t &Res);
159
160 /// }
161
162private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000163 void CheckForValidSection();
164
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000165 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000166 void EatToEndOfLine();
167 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000168
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000169 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000170 bool expandMacro(SmallString<256> &Buf, StringRef Body,
171 const std::vector<StringRef> &Parameters,
172 const std::vector<std::vector<AsmToken> > &A,
173 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000174 void HandleMacroExit();
175
176 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000177 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000178 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
179 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000180 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000181 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000182
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000183 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
184 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000185 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
186 /// This returns true on failure.
187 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000188
189 /// \brief Reset the current lexer position to that given by \arg Loc. The
190 /// current token is not set; clients should ensure Lex() is called
191 /// subsequently.
192 void JumpToLoc(SMLoc Loc);
193
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000194 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000195
196 /// \brief Parse up to the end of statement and a return the contents from the
197 /// current token until the end of the statement; the current token on exit
198 /// will be either the EndOfStatement or EOF.
199 StringRef ParseStringToEndOfStatement();
200
Nico Weber4c4c7322011-01-28 03:04:41 +0000201 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000202
203 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
204 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
205 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000206 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000207
208 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
209 /// and set \arg Res to the identifier contents.
210 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000211
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000212 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000213
214 // ".ascii", ".asciiz", ".string"
215 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000216 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000217 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000218 bool ParseDirectiveFill(); // ".fill"
219 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000220 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000221 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000222 bool ParseDirectiveOrg(); // ".org"
223 // ".align{,32}", ".p2align{,w,l}"
224 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
225
226 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
227 /// accepts a single symbol (which should be a label or an external).
228 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000229
230 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
231
232 bool ParseDirectiveAbort(); // ".abort"
233 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000234 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000235
236 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000237 // ".ifdef" or ".ifndef", depending on expect_defined
238 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000239 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
240 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
241 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
242
243 /// ParseEscapedString - Parse the current token as a string which may include
244 /// escaped characters and return the string contents.
245 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000246
247 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
248 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000249};
250
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000251/// \brief Generic implementations of directive handling, etc. which is shared
252/// (or the default, at least) for all assembler parser.
253class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000254 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
255 void AddDirectiveHandler(StringRef Directive) {
256 getParser().AddDirectiveHandler(this, Directive,
257 HandleDirective<GenericAsmParser, Handler>);
258 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000259public:
260 GenericAsmParser() {}
261
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000262 AsmParser &getParser() {
263 return (AsmParser&) this->MCAsmParserExtension::getParser();
264 }
265
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000266 virtual void Initialize(MCAsmParser &Parser) {
267 // Call the base implementation.
268 this->MCAsmParserExtension::Initialize(Parser);
269
270 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000271 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
273 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000275
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000276 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000277 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
278 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000279 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
280 ".cfi_startproc");
281 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
282 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000283 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
284 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000285 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
286 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000287 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
288 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000289 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
290 ".cfi_def_cfa_register");
291 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
292 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000293 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
294 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000295 AddDirectiveHandler<
296 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
297 AddDirectiveHandler<
298 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000299 AddDirectiveHandler<
300 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
301 AddDirectiveHandler<
302 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000303 AddDirectiveHandler<
304 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000305 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000306 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
307 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000308 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000309
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000310 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000311 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
312 ".macros_on");
313 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
314 ".macros_off");
315 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
316 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
317 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000318
319 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
320 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000321 }
322
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000323 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
324
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000325 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
326 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
327 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000328 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000329 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000330 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
331 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000332 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000333 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000334 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000335 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
336 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000337 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000338 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000339 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
340 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000341 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000342 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000343 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000344
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000345 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000346 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
347 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000348
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000349 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000350};
351
352}
353
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000354namespace llvm {
355
356extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000357extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000358extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000359
360}
361
Chris Lattneraaec2052010-01-19 19:46:13 +0000362enum { DEFAULT_ADDRSPACE = 0 };
363
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000364AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000365 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000366 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000367 GenericParser(new GenericAsmParser), PlatformParser(0),
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000368 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000369 // Save the old handler.
370 SavedDiagHandler = SrcMgr.getDiagHandler();
371 SavedDiagContext = SrcMgr.getDiagContext();
372 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000373 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000374 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000375
376 // Initialize the generic parser.
377 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000378
379 // Initialize the platform / file format parser.
380 //
381 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
382 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000383 if (_MAI.hasMicrosoftFastStdCallMangling()) {
384 PlatformParser = createCOFFAsmParser();
385 PlatformParser->Initialize(*this);
386 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000387 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000388 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000389 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000390 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000391 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000392 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000393}
394
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000395AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000396 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
397
398 // Destroy any macros.
399 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
400 ie = MacroMap.end(); it != ie; ++it)
401 delete it->getValue();
402
Daniel Dunbare4749702010-07-12 18:12:02 +0000403 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000404 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000405}
406
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000407void AsmParser::PrintMacroInstantiations() {
408 // Print the active macro instantiation stack.
409 for (std::vector<MacroInstantiation*>::const_reverse_iterator
410 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000411 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
412 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000413}
414
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000415bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000416 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000417 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000418 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000419 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000420 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000421}
422
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000423bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000424 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000425 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000426 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000427 return true;
428}
429
Sean Callananfd0b0282010-01-21 00:19:58 +0000430bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000431 std::string IncludedFile;
432 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000433 if (NewBuf == -1)
434 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000435
Sean Callananfd0b0282010-01-21 00:19:58 +0000436 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000437
Sean Callananfd0b0282010-01-21 00:19:58 +0000438 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000439
Sean Callananfd0b0282010-01-21 00:19:58 +0000440 return false;
441}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000442
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000443/// Process the specified .incbin file by seaching for it in the include paths
444/// then just emiting the byte contents of the file to the streamer. This
445/// returns true on failure.
446bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
447 std::string IncludedFile;
448 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
449 if (NewBuf == -1)
450 return true;
451
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000452 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000453 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
454 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000455 return false;
456}
457
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000458void AsmParser::JumpToLoc(SMLoc Loc) {
459 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
460 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
461}
462
Sean Callananfd0b0282010-01-21 00:19:58 +0000463const AsmToken &AsmParser::Lex() {
464 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000465
Sean Callananfd0b0282010-01-21 00:19:58 +0000466 if (tok->is(AsmToken::Eof)) {
467 // If this is the end of an included file, pop the parent file off the
468 // include stack.
469 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
470 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000471 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000472 tok = &Lexer.Lex();
473 }
474 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000475
Sean Callananfd0b0282010-01-21 00:19:58 +0000476 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000477 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000478
Sean Callananfd0b0282010-01-21 00:19:58 +0000479 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000480}
481
Chris Lattner79180e22010-04-05 23:15:42 +0000482bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000483 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000484 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000485 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000486
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000487 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000488 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000489
490 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000491 AsmCond StartingCondState = TheCondState;
492
Kevin Enderby613b7572011-11-01 22:27:22 +0000493 // If we are generating dwarf for assembly source files save the initial text
494 // section and generate a .file directive.
495 if (getContext().getGenDwarfForAssembly()) {
496 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000497 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
498 getStreamer().EmitLabel(SectionStartSym);
499 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000500 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
501 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
502 }
503
Chris Lattnerb717fb02009-07-02 21:53:43 +0000504 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000505 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000506 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000507
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000508 // We had an error, validate that one was emitted and recover by skipping to
509 // the next line.
510 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000511 EatToEndOfStatement();
512 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000513
514 if (TheCondState.TheCond != StartingCondState.TheCond ||
515 TheCondState.Ignore != StartingCondState.Ignore)
516 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000517
518 // Check to see there are no empty DwarfFile slots.
519 const std::vector<MCDwarfFile *> &MCDwarfFiles =
520 getContext().getMCDwarfFiles();
521 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000522 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000523 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000524 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000525
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000526 // Check to see that all assembler local symbols were actually defined.
527 // Targets that don't do subsections via symbols may not want this, though,
528 // so conservatively exclude them. Only do this if we're finalizing, though,
529 // as otherwise we won't necessarilly have seen everything yet.
530 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
531 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
532 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
533 e = Symbols.end();
534 i != e; ++i) {
535 MCSymbol *Sym = i->getValue();
536 // Variable symbols may not be marked as defined, so check those
537 // explicitly. If we know it's a variable, we have a definition for
538 // the purposes of this check.
539 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
540 // FIXME: We would really like to refer back to where the symbol was
541 // first referenced for a source location. We need to add something
542 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000543 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
544 "assembler local symbol '" + Sym->getName() +
545 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000546 }
547 }
548
549
Chris Lattner79180e22010-04-05 23:15:42 +0000550 // Finalize the output stream if there are no errors and if the client wants
551 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000552 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000553 Out.Finish();
554
Chris Lattnerb717fb02009-07-02 21:53:43 +0000555 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000556}
557
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000558void AsmParser::CheckForValidSection() {
559 if (!getStreamer().getCurrentSection()) {
560 TokError("expected section directive before assembly directive");
561 Out.SwitchSection(Ctx.getMachOSection(
562 "__TEXT", "__text",
563 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
564 0, SectionKind::getText()));
565 }
566}
567
Chris Lattner2cf5f142009-06-22 01:29:09 +0000568/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
569void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000570 while (Lexer.isNot(AsmToken::EndOfStatement) &&
571 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000572 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000573
Chris Lattner2cf5f142009-06-22 01:29:09 +0000574 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000575 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000576 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000577}
578
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000579StringRef AsmParser::ParseStringToEndOfStatement() {
580 const char *Start = getTok().getLoc().getPointer();
581
582 while (Lexer.isNot(AsmToken::EndOfStatement) &&
583 Lexer.isNot(AsmToken::Eof))
584 Lex();
585
586 const char *End = getTok().getLoc().getPointer();
587 return StringRef(Start, End - Start);
588}
Chris Lattnerc4193832009-06-22 05:51:26 +0000589
Chris Lattner74ec1a32009-06-22 06:32:03 +0000590/// ParseParenExpr - Parse a paren expression and return it.
591/// NOTE: This assumes the leading '(' has already been consumed.
592///
593/// parenexpr ::= expr)
594///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000595bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000596 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000597 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000598 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000599 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000600 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000601 return false;
602}
Chris Lattnerc4193832009-06-22 05:51:26 +0000603
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000604/// ParseBracketExpr - Parse a bracket expression and return it.
605/// NOTE: This assumes the leading '[' has already been consumed.
606///
607/// bracketexpr ::= expr]
608///
609bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
610 if (ParseExpression(Res)) return true;
611 if (Lexer.isNot(AsmToken::RBrac))
612 return TokError("expected ']' in brackets expression");
613 EndLoc = Lexer.getLoc();
614 Lex();
615 return false;
616}
617
Chris Lattner74ec1a32009-06-22 06:32:03 +0000618/// ParsePrimaryExpr - Parse a primary expression and return it.
619/// primaryexpr ::= (parenexpr
620/// primaryexpr ::= symbol
621/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000622/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000623/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000624bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000625 switch (Lexer.getKind()) {
626 default:
627 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000628 // If we have an error assume that we've already handled it.
629 case AsmToken::Error:
630 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000631 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000632 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000633 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000634 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000635 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000636 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000637 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000638 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000639 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000640 EndLoc = Lexer.getLoc();
641
642 StringRef Identifier;
643 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000644 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000645
Daniel Dunbarfffff912009-10-16 01:34:54 +0000646 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000647 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000648 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000649
650 // Lookup the symbol variant if used.
651 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000652 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000653 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000654 if (Variant == MCSymbolRefExpr::VK_Invalid) {
655 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000656 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000657 }
658 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000659
Daniel Dunbarfffff912009-10-16 01:34:54 +0000660 // If this is an absolute variable reference, substitute it now to preserve
661 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000662 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000663 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000664 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000665
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000666 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000667 return false;
668 }
669
670 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000671 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000672 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000673 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000674 case AsmToken::Integer: {
675 SMLoc Loc = getTok().getLoc();
676 int64_t IntVal = getTok().getIntVal();
677 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000678 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000679 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000680 // Look for 'b' or 'f' following an Integer as a directional label
681 if (Lexer.getKind() == AsmToken::Identifier) {
682 StringRef IDVal = getTok().getString();
683 if (IDVal == "f" || IDVal == "b"){
684 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
685 IDVal == "f" ? 1 : 0);
686 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
687 getContext());
688 if(IDVal == "b" && Sym->isUndefined())
689 return Error(Loc, "invalid reference to undefined symbol");
690 EndLoc = Lexer.getLoc();
691 Lex(); // Eat identifier.
692 }
693 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000694 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000695 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000696 case AsmToken::Real: {
697 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000698 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000699 Res = MCConstantExpr::Create(IntVal, getContext());
700 Lex(); // Eat token.
701 return false;
702 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000703 case AsmToken::Dot: {
704 // This is a '.' reference, which references the current PC. Emit a
705 // temporary label to the streamer and refer to it.
706 MCSymbol *Sym = Ctx.CreateTempSymbol();
707 Out.EmitLabel(Sym);
708 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
709 EndLoc = Lexer.getLoc();
710 Lex(); // Eat identifier.
711 return false;
712 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000713 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000714 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000715 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000716 case AsmToken::LBrac:
717 if (!PlatformParser->HasBracketExpressions())
718 return TokError("brackets expression not supported on this target");
719 Lex(); // Eat the '['.
720 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000721 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000722 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000723 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000724 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000725 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000726 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000727 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000728 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000729 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000730 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000731 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000732 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000733 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000734 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000735 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000736 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000737 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000738 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000739 }
740}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000741
Chris Lattnerb4307b32010-01-15 19:28:38 +0000742bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000743 SMLoc EndLoc;
744 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000745}
746
Daniel Dunbarcceba832010-09-17 02:47:07 +0000747const MCExpr *
748AsmParser::ApplyModifierToExpr(const MCExpr *E,
749 MCSymbolRefExpr::VariantKind Variant) {
750 // Recurse over the given expression, rebuilding it to apply the given variant
751 // if there is exactly one symbol.
752 switch (E->getKind()) {
753 case MCExpr::Target:
754 case MCExpr::Constant:
755 return 0;
756
757 case MCExpr::SymbolRef: {
758 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
759
760 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
761 TokError("invalid variant on expression '" +
762 getTok().getIdentifier() + "' (already modified)");
763 return E;
764 }
765
766 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
767 }
768
769 case MCExpr::Unary: {
770 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
771 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
772 if (!Sub)
773 return 0;
774 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
775 }
776
777 case MCExpr::Binary: {
778 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
779 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
780 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
781
782 if (!LHS && !RHS)
783 return 0;
784
785 if (!LHS) LHS = BE->getLHS();
786 if (!RHS) RHS = BE->getRHS();
787
788 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
789 }
790 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000791
792 assert(0 && "Invalid expression kind!");
793 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000794}
795
Chris Lattner74ec1a32009-06-22 06:32:03 +0000796/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000797///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000798/// expr ::= expr &&,|| expr -> lowest.
799/// expr ::= expr |,^,&,! expr
800/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
801/// expr ::= expr <<,>> expr
802/// expr ::= expr +,- expr
803/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000804/// expr ::= primaryexpr
805///
Chris Lattner54482b42010-01-15 19:39:23 +0000806bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000807 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000808 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000809 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
810 return true;
811
Daniel Dunbarcceba832010-09-17 02:47:07 +0000812 // As a special case, we support 'a op b @ modifier' by rewriting the
813 // expression to include the modifier. This is inefficient, but in general we
814 // expect users to use 'a@modifier op b'.
815 if (Lexer.getKind() == AsmToken::At) {
816 Lex();
817
818 if (Lexer.isNot(AsmToken::Identifier))
819 return TokError("unexpected symbol modifier following '@'");
820
821 MCSymbolRefExpr::VariantKind Variant =
822 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
823 if (Variant == MCSymbolRefExpr::VK_Invalid)
824 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
825
826 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
827 if (!ModifiedRes) {
828 return TokError("invalid modifier '" + getTok().getIdentifier() +
829 "' (no symbols present)");
830 return true;
831 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000832
Daniel Dunbarcceba832010-09-17 02:47:07 +0000833 Res = ModifiedRes;
834 Lex();
835 }
836
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000837 // Try to constant fold it up front, if possible.
838 int64_t Value;
839 if (Res->EvaluateAsAbsolute(Value))
840 Res = MCConstantExpr::Create(Value, getContext());
841
842 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000843}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000844
Chris Lattnerb4307b32010-01-15 19:28:38 +0000845bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000846 Res = 0;
847 return ParseParenExpr(Res, EndLoc) ||
848 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000849}
850
Daniel Dunbar475839e2009-06-29 20:37:27 +0000851bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000852 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000853
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000854 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000855 if (ParseExpression(Expr))
856 return true;
857
Daniel Dunbare00b0112009-10-16 01:57:52 +0000858 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000859 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000860
861 return false;
862}
863
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000864static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000865 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000866 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000867 default:
868 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000869
Jim Grosbachfbe16812011-08-20 16:24:13 +0000870 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000871 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000872 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000873 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000874 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000875 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000876 return 1;
877
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000878
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000879 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000880 //
881 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000882 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000883 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000884 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000885 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000886 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000887 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000888 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000889 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000890 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000891
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000892 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000893 case AsmToken::EqualEqual:
894 Kind = MCBinaryExpr::EQ;
895 return 3;
896 case AsmToken::ExclaimEqual:
897 case AsmToken::LessGreater:
898 Kind = MCBinaryExpr::NE;
899 return 3;
900 case AsmToken::Less:
901 Kind = MCBinaryExpr::LT;
902 return 3;
903 case AsmToken::LessEqual:
904 Kind = MCBinaryExpr::LTE;
905 return 3;
906 case AsmToken::Greater:
907 Kind = MCBinaryExpr::GT;
908 return 3;
909 case AsmToken::GreaterEqual:
910 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000911 return 3;
912
Jim Grosbachfbe16812011-08-20 16:24:13 +0000913 // Intermediate Precedence: <<, >>
914 case AsmToken::LessLess:
915 Kind = MCBinaryExpr::Shl;
916 return 4;
917 case AsmToken::GreaterGreater:
918 Kind = MCBinaryExpr::Shr;
919 return 4;
920
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000921 // High Intermediate Precedence: +, -
922 case AsmToken::Plus:
923 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000924 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000925 case AsmToken::Minus:
926 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000927 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000928
Jim Grosbachfbe16812011-08-20 16:24:13 +0000929 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000930 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000931 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000932 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000933 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000934 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000935 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000936 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000937 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000938 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000939 }
940}
941
942
943/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
944/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000945bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
946 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000947 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000948 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000949 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000950
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000951 // If the next token is lower precedence than we are allowed to eat, return
952 // successfully with what we ate already.
953 if (TokPrec < Precedence)
954 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000955
Sean Callanan79ed1a82010-01-19 20:22:31 +0000956 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000957
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000958 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000959 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000960 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000961
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000962 // If BinOp binds less tightly with RHS than the operator after RHS, let
963 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000964 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000965 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000966 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000967 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000968 }
969
Daniel Dunbar475839e2009-06-29 20:37:27 +0000970 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000971 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000972 }
973}
974
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000975
976
977
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000978/// ParseStatement:
979/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000980/// ::= Label* Directive ...Operands... EndOfStatement
981/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000982bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000983 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000984 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000985 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000986 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000987 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000988
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000989 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000990 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000991 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000992 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000993 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000994 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000995 if (Lexer.is(AsmToken::Hash))
996 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000997
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000998 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000999 if (Lexer.is(AsmToken::Integer)) {
1000 LocalLabelVal = getTok().getIntVal();
1001 if (LocalLabelVal < 0) {
1002 if (!TheCondState.Ignore)
1003 return TokError("unexpected token at start of statement");
1004 IDVal = "";
1005 }
1006 else {
1007 IDVal = getTok().getString();
1008 Lex(); // Consume the integer token to be used as an identifier token.
1009 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001010 if (!TheCondState.Ignore)
1011 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001012 }
1013 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001014
1015 } else if (Lexer.is(AsmToken::Dot)) {
1016 // Treat '.' as a valid identifier in this context.
1017 Lex();
1018 IDVal = ".";
1019
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001020 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001021 if (!TheCondState.Ignore)
1022 return TokError("unexpected token at start of statement");
1023 IDVal = "";
1024 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001025
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001026
Chris Lattner7834fac2010-04-17 18:14:27 +00001027 // Handle conditional assembly here before checking for skipping. We
1028 // have to do this so that .endif isn't skipped in a ".if 0" block for
1029 // example.
1030 if (IDVal == ".if")
1031 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001032 if (IDVal == ".ifdef")
1033 return ParseDirectiveIfdef(IDLoc, true);
1034 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1035 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001036 if (IDVal == ".elseif")
1037 return ParseDirectiveElseIf(IDLoc);
1038 if (IDVal == ".else")
1039 return ParseDirectiveElse(IDLoc);
1040 if (IDVal == ".endif")
1041 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001042
Chris Lattner7834fac2010-04-17 18:14:27 +00001043 // If we are in a ".if 0" block, ignore this statement.
1044 if (TheCondState.Ignore) {
1045 EatToEndOfStatement();
1046 return false;
1047 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001048
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001049 // FIXME: Recurse on local labels?
1050
1051 // See what kind of statement we have.
1052 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001053 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001054 CheckForValidSection();
1055
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001056 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001057 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001058
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001059 // Diagnose attempt to use '.' as a label.
1060 if (IDVal == ".")
1061 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1062
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001063 // Diagnose attempt to use a variable as a label.
1064 //
1065 // FIXME: Diagnostics. Note the location of the definition as a label.
1066 // FIXME: This doesn't diagnose assignment to a symbol which has been
1067 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001068 MCSymbol *Sym;
1069 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001070 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001071 else
1072 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001073 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001074 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001075
Daniel Dunbar959fd882009-08-26 22:13:22 +00001076 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001077 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001078
Kevin Enderby94c2e852011-12-09 18:09:40 +00001079 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001080 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001081 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001082 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1083 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001084
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001085 // Consume any end of statement token, if present, to avoid spurious
1086 // AddBlankLine calls().
1087 if (Lexer.is(AsmToken::EndOfStatement)) {
1088 Lex();
1089 if (Lexer.is(AsmToken::Eof))
1090 return false;
1091 }
1092
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001093 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001094 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001095
Daniel Dunbar3f872332009-07-28 16:08:33 +00001096 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001097 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001098 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001099
Nico Weber4c4c7322011-01-28 03:04:41 +00001100 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001101
1102 default: // Normal instruction or directive.
1103 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001104 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001105
1106 // If macros are enabled, check to see if this is a macro instantiation.
1107 if (MacrosEnabled)
1108 if (const Macro *M = MacroMap.lookup(IDVal))
1109 return HandleMacroEntry(IDVal, IDLoc, M);
1110
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001111 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001112 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001113 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001114 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001115 return ParseDirectiveSet(IDVal, true);
1116 if (IDVal == ".equiv")
1117 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001118
Daniel Dunbara0d14262009-06-24 23:30:00 +00001119 // Data directives
1120
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001121 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001122 return ParseDirectiveAscii(IDVal, false);
1123 if (IDVal == ".asciz" || IDVal == ".string")
1124 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001125
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001126 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001127 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001128 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001129 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001130 if (IDVal == ".value")
1131 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001132 if (IDVal == ".2byte")
1133 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001134 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001135 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001136 if (IDVal == ".int")
1137 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001138 if (IDVal == ".4byte")
1139 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001140 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001141 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001142 if (IDVal == ".8byte")
1143 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001144 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001145 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1146 if (IDVal == ".double")
1147 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001148
Eli Friedman5d68ec22010-07-19 04:17:25 +00001149 if (IDVal == ".align") {
1150 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1151 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1152 }
1153 if (IDVal == ".align32") {
1154 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1155 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1156 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001157 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001158 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001159 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001160 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001161 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001162 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001163 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001164 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001165 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001166 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001167 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001168 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1169
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001170 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001171 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001172
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001173 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001174 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001175 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001176 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001177 if (IDVal == ".zero")
1178 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001179
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001180 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001181
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001182 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001183 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001184 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001185 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001186 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001187 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001188 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001189 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001190 if (IDVal == ".symbol_resolver")
1191 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001192 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001193 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001194 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001195 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001196 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001197 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001198 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001199 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001200 if (IDVal == ".weak_def_can_be_hidden")
1201 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001202
Hans Wennborg5cc64912011-06-18 13:51:54 +00001203 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001204 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001205 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001206 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001207
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001208 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001209 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001210 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001211 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001212 if (IDVal == ".incbin")
1213 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001214
Evan Chengbd27f5a2011-07-27 00:38:12 +00001215 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001216 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001217
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001218 // Look up the handler in the handler table.
1219 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1220 DirectiveMap.lookup(IDVal);
1221 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001222 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001223
Kevin Enderby9c656452009-09-10 20:51:44 +00001224 // Target hook for parsing target specific directives.
1225 if (!getTargetParser().ParseDirective(ID))
1226 return false;
1227
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001228 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001229 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001230 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001231 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001232
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001233 CheckForValidSection();
1234
Chris Lattnera7f13542010-05-19 23:34:33 +00001235 // Canonicalize the opcode to lower case.
1236 SmallString<128> Opcode;
1237 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1238 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001239
Chris Lattner98986712010-01-14 22:21:20 +00001240 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001241 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001242 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001243
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001244 // Dump the parsed representation, if requested.
1245 if (getShowParsedOperands()) {
1246 SmallString<256> Str;
1247 raw_svector_ostream OS(Str);
1248 OS << "parsed instruction: [";
1249 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1250 if (i != 0)
1251 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001252 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001253 }
1254 OS << "]";
1255
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001256 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001257 }
1258
Kevin Enderby613b7572011-11-01 22:27:22 +00001259 // If we are generating dwarf for assembly source files and the current
1260 // section is the initial text section then generate a .loc directive for
1261 // the instruction.
1262 if (!HadError && getContext().getGenDwarfForAssembly() &&
1263 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1264 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1265 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1266 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001267 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001268 StringRef());
1269 }
1270
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001271 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001272 if (!HadError)
1273 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1274 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001275
Chris Lattner98986712010-01-14 22:21:20 +00001276 // Free any parsed operands.
1277 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1278 delete ParsedOperands[i];
1279
Chris Lattnercbf8a982010-09-11 16:18:25 +00001280 // Don't skip the rest of the line, the instruction parser is responsible for
1281 // that.
1282 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001283}
Chris Lattner9a023f72009-06-24 04:43:34 +00001284
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001285/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1286/// since they may not be able to be tokenized to get to the end of line token.
1287void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001288 if (!Lexer.is(AsmToken::EndOfStatement))
1289 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001290 // Eat EOL.
1291 Lex();
1292}
1293
1294/// ParseCppHashLineFilenameComment as this:
1295/// ::= # number "filename"
1296/// or just as a full line comment if it doesn't have a number and a string.
1297bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1298 Lex(); // Eat the hash token.
1299
1300 if (getLexer().isNot(AsmToken::Integer)) {
1301 // Consume the line since in cases it is not a well-formed line directive,
1302 // as if were simply a full line comment.
1303 EatToEndOfLine();
1304 return false;
1305 }
1306
1307 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001308 Lex();
1309
1310 if (getLexer().isNot(AsmToken::String)) {
1311 EatToEndOfLine();
1312 return false;
1313 }
1314
1315 StringRef Filename = getTok().getString();
1316 // Get rid of the enclosing quotes.
1317 Filename = Filename.substr(1, Filename.size()-2);
1318
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001319 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1320 CppHashLoc = L;
1321 CppHashFilename = Filename;
1322 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001323
1324 // Ignore any trailing characters, they're just comment.
1325 EatToEndOfLine();
1326 return false;
1327}
1328
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001329/// DiagHandler - will use the the last parsed cpp hash line filename comment
1330/// for the Filename and LineNo if any in the diagnostic.
1331void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1332 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1333 raw_ostream &OS = errs();
1334
1335 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1336 const SMLoc &DiagLoc = Diag.getLoc();
1337 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1338 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1339
1340 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1341 // before printing the message.
1342 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001343 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001344 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1345 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1346 }
1347
1348 // If we have not parsed a cpp hash line filename comment or the source
1349 // manager changed or buffer changed (like in a nested include) then just
1350 // print the normal diagnostic using its Filename and LineNo.
1351 if (!Parser->CppHashLineNumber ||
1352 &DiagSrcMgr != &Parser->SrcMgr ||
1353 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001354 if (Parser->SavedDiagHandler)
1355 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1356 else
1357 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001358 return;
1359 }
1360
1361 // Use the CppHashFilename and calculate a line number based on the
1362 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1363 // the diagnostic.
1364 const std::string Filename = Parser->CppHashFilename;
1365
1366 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1367 int CppHashLocLineNo =
1368 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1369 int LineNo = Parser->CppHashLineNumber - 1 +
1370 (DiagLocLineNo - CppHashLocLineNo);
1371
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001372 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1373 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001374 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001375 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001376
Benjamin Kramer04a04262011-10-16 10:48:29 +00001377 if (Parser->SavedDiagHandler)
1378 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1379 else
1380 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001381}
1382
Rafael Espindola65366442011-06-05 02:43:45 +00001383bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1384 const std::vector<StringRef> &Parameters,
1385 const std::vector<std::vector<AsmToken> > &A,
1386 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001387 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001388 unsigned NParameters = Parameters.size();
1389 if (NParameters != 0 && NParameters != A.size())
1390 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001391
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001392 while (!Body.empty()) {
1393 // Scan for the next substitution.
1394 std::size_t End = Body.size(), Pos = 0;
1395 for (; Pos != End; ++Pos) {
1396 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001397 if (!NParameters) {
1398 // This macro has no parameters, look for $0, $1, etc.
1399 if (Body[Pos] != '$' || Pos + 1 == End)
1400 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001401
Rafael Espindola65366442011-06-05 02:43:45 +00001402 char Next = Body[Pos + 1];
1403 if (Next == '$' || Next == 'n' || isdigit(Next))
1404 break;
1405 } else {
1406 // This macro has parameters, look for \foo, \bar, etc.
1407 if (Body[Pos] == '\\' && Pos + 1 != End)
1408 break;
1409 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001410 }
1411
1412 // Add the prefix.
1413 OS << Body.slice(0, Pos);
1414
1415 // Check if we reached the end.
1416 if (Pos == End)
1417 break;
1418
Rafael Espindola65366442011-06-05 02:43:45 +00001419 if (!NParameters) {
1420 switch (Body[Pos+1]) {
1421 // $$ => $
1422 case '$':
1423 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001424 break;
1425
Rafael Espindola65366442011-06-05 02:43:45 +00001426 // $n => number of arguments
1427 case 'n':
1428 OS << A.size();
1429 break;
1430
1431 // $[0-9] => argument
1432 default: {
1433 // Missing arguments are ignored.
1434 unsigned Index = Body[Pos+1] - '0';
1435 if (Index >= A.size())
1436 break;
1437
1438 // Otherwise substitute with the token values, with spaces eliminated.
1439 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1440 ie = A[Index].end(); it != ie; ++it)
1441 OS << it->getString();
1442 break;
1443 }
1444 }
1445 Pos += 2;
1446 } else {
1447 unsigned I = Pos + 1;
1448 while (isalnum(Body[I]) && I + 1 != End)
1449 ++I;
1450
1451 const char *Begin = Body.data() + Pos +1;
1452 StringRef Argument(Begin, I - (Pos +1));
1453 unsigned Index = 0;
1454 for (; Index < NParameters; ++Index)
1455 if (Parameters[Index] == Argument)
1456 break;
1457
1458 // FIXME: We should error at the macro definition.
1459 if (Index == NParameters)
1460 return Error(L, "Parameter not found");
1461
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001462 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1463 ie = A[Index].end(); it != ie; ++it)
1464 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001465
Rafael Espindola65366442011-06-05 02:43:45 +00001466 Pos += 1 + Argument.size();
1467 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001468 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001469 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001470 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001471
1472 // We include the .endmacro in the buffer as our queue to exit the macro
1473 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001474 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001475 return false;
1476}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001477
Rafael Espindola65366442011-06-05 02:43:45 +00001478MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1479 MemoryBuffer *I)
1480 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1481{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001482}
1483
1484bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1485 const Macro *M) {
1486 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1487 // this, although we should protect against infinite loops.
1488 if (ActiveMacros.size() == 20)
1489 return TokError("macros cannot be nested more than 20 levels deep");
1490
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001491 // Parse the macro instantiation arguments.
1492 std::vector<std::vector<AsmToken> > MacroArguments;
1493 MacroArguments.push_back(std::vector<AsmToken>());
1494 unsigned ParenLevel = 0;
1495 for (;;) {
1496 if (Lexer.is(AsmToken::Eof))
1497 return TokError("unexpected token in macro instantiation");
1498 if (Lexer.is(AsmToken::EndOfStatement))
1499 break;
1500
1501 // If we aren't inside parentheses and this is a comma, start a new token
1502 // list.
1503 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1504 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001505 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001506 // Adjust the current parentheses level.
1507 if (Lexer.is(AsmToken::LParen))
1508 ++ParenLevel;
1509 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1510 --ParenLevel;
1511
1512 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001513 MacroArguments.back().push_back(getTok());
1514 }
1515 Lex();
1516 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001517
Rafael Espindola65366442011-06-05 02:43:45 +00001518 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1519 // to hold the macro body with substitutions.
1520 SmallString<256> Buf;
1521 StringRef Body = M->Body;
1522
1523 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1524 return true;
1525
1526 MemoryBuffer *Instantiation =
1527 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1528
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001529 // Create the macro instantiation object and add to the current macro
1530 // instantiation stack.
1531 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001532 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001533 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001534 ActiveMacros.push_back(MI);
1535
1536 // Jump to the macro instantiation and prime the lexer.
1537 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1538 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1539 Lex();
1540
1541 return false;
1542}
1543
1544void AsmParser::HandleMacroExit() {
1545 // Jump to the EndOfStatement we should return to, and consume it.
1546 JumpToLoc(ActiveMacros.back()->ExitLoc);
1547 Lex();
1548
1549 // Pop the instantiation entry.
1550 delete ActiveMacros.back();
1551 ActiveMacros.pop_back();
1552}
1553
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001554static void MarkUsed(const MCExpr *Value) {
1555 switch (Value->getKind()) {
1556 case MCExpr::Binary:
1557 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1558 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1559 break;
1560 case MCExpr::Target:
1561 case MCExpr::Constant:
1562 break;
1563 case MCExpr::SymbolRef: {
1564 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1565 break;
1566 }
1567 case MCExpr::Unary:
1568 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1569 break;
1570 }
1571}
1572
Nico Weber4c4c7322011-01-28 03:04:41 +00001573bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001574 // FIXME: Use better location, we should use proper tokens.
1575 SMLoc EqualLoc = Lexer.getLoc();
1576
Daniel Dunbar821e3332009-08-31 08:09:28 +00001577 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001578 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001579 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001580
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001581 MarkUsed(Value);
1582
Daniel Dunbar3f872332009-07-28 16:08:33 +00001583 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001584 return TokError("unexpected token in assignment");
1585
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001586 // Error on assignment to '.'.
1587 if (Name == ".") {
1588 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1589 "(use '.space' or '.org').)"));
1590 }
1591
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001592 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001593 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001594
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001595 // Validate that the LHS is allowed to be a variable (either it has not been
1596 // used as a symbol, or it is an absolute symbol).
1597 MCSymbol *Sym = getContext().LookupSymbol(Name);
1598 if (Sym) {
1599 // Diagnose assignment to a label.
1600 //
1601 // FIXME: Diagnostics. Note the location of the definition as a label.
1602 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001603 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001604 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001605 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001606 return Error(EqualLoc, "redefinition of '" + Name + "'");
1607 else if (!Sym->isVariable())
1608 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001609 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001610 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1611 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001612
1613 // Don't count these checks as uses.
1614 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001615 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001616 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001617
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001618 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001619
1620 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001621 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001622
1623 return false;
1624}
1625
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001626/// ParseIdentifier:
1627/// ::= identifier
1628/// ::= string
1629bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001630 // The assembler has relaxed rules for accepting identifiers, in particular we
1631 // allow things like '.globl $foo', which would normally be separate
1632 // tokens. At this level, we have already lexed so we cannot (currently)
1633 // handle this as a context dependent token, instead we detect adjacent tokens
1634 // and return the combined identifier.
1635 if (Lexer.is(AsmToken::Dollar)) {
1636 SMLoc DollarLoc = getLexer().getLoc();
1637
1638 // Consume the dollar sign, and check for a following identifier.
1639 Lex();
1640 if (Lexer.isNot(AsmToken::Identifier))
1641 return true;
1642
1643 // We have a '$' followed by an identifier, make sure they are adjacent.
1644 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1645 return true;
1646
1647 // Construct the joined identifier and consume the token.
1648 Res = StringRef(DollarLoc.getPointer(),
1649 getTok().getIdentifier().size() + 1);
1650 Lex();
1651 return false;
1652 }
1653
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001654 if (Lexer.isNot(AsmToken::Identifier) &&
1655 Lexer.isNot(AsmToken::String))
1656 return true;
1657
Sean Callanan18b83232010-01-19 21:44:56 +00001658 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001659
Sean Callanan79ed1a82010-01-19 20:22:31 +00001660 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001661
1662 return false;
1663}
1664
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001665/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001666/// ::= .equ identifier ',' expression
1667/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001668/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001669bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001670 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001671
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001672 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001673 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001674
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001675 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001676 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001677 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001678
Nico Weber4c4c7322011-01-28 03:04:41 +00001679 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001680}
1681
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001682bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001683 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001684
1685 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001686 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001687 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1688 if (Str[i] != '\\') {
1689 Data += Str[i];
1690 continue;
1691 }
1692
1693 // Recognize escaped characters. Note that this escape semantics currently
1694 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1695 ++i;
1696 if (i == e)
1697 return TokError("unexpected backslash at end of string");
1698
1699 // Recognize octal sequences.
1700 if ((unsigned) (Str[i] - '0') <= 7) {
1701 // Consume up to three octal characters.
1702 unsigned Value = Str[i] - '0';
1703
1704 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1705 ++i;
1706 Value = Value * 8 + (Str[i] - '0');
1707
1708 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1709 ++i;
1710 Value = Value * 8 + (Str[i] - '0');
1711 }
1712 }
1713
1714 if (Value > 255)
1715 return TokError("invalid octal escape sequence (out of range)");
1716
1717 Data += (unsigned char) Value;
1718 continue;
1719 }
1720
1721 // Otherwise recognize individual escapes.
1722 switch (Str[i]) {
1723 default:
1724 // Just reject invalid escape sequences for now.
1725 return TokError("invalid escape sequence (unrecognized character)");
1726
1727 case 'b': Data += '\b'; break;
1728 case 'f': Data += '\f'; break;
1729 case 'n': Data += '\n'; break;
1730 case 'r': Data += '\r'; break;
1731 case 't': Data += '\t'; break;
1732 case '"': Data += '"'; break;
1733 case '\\': Data += '\\'; break;
1734 }
1735 }
1736
1737 return false;
1738}
1739
Daniel Dunbara0d14262009-06-24 23:30:00 +00001740/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001741/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1742bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001743 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001744 CheckForValidSection();
1745
Daniel Dunbara0d14262009-06-24 23:30:00 +00001746 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001747 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001748 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001749
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001750 std::string Data;
1751 if (ParseEscapedString(Data))
1752 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001753
1754 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001755 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001756 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1757
Sean Callanan79ed1a82010-01-19 20:22:31 +00001758 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001759
1760 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001761 break;
1762
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001763 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001764 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001765 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001766 }
1767 }
1768
Sean Callanan79ed1a82010-01-19 20:22:31 +00001769 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001770 return false;
1771}
1772
1773/// ParseDirectiveValue
1774/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1775bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001776 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001777 CheckForValidSection();
1778
Daniel Dunbara0d14262009-06-24 23:30:00 +00001779 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001780 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001781 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001782 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001783 return true;
1784
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001785 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001786 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1787 assert(Size <= 8 && "Invalid size");
1788 uint64_t IntValue = MCE->getValue();
1789 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1790 return Error(ExprLoc, "literal value out of range for directive");
1791 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1792 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001793 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001794
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001795 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001796 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001797
Daniel Dunbara0d14262009-06-24 23:30:00 +00001798 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001799 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001800 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001801 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001802 }
1803 }
1804
Sean Callanan79ed1a82010-01-19 20:22:31 +00001805 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001806 return false;
1807}
1808
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001809/// ParseDirectiveRealValue
1810/// ::= (.single | .double) [ expression (, expression)* ]
1811bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1812 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1813 CheckForValidSection();
1814
1815 for (;;) {
1816 // We don't truly support arithmetic on floating point expressions, so we
1817 // have to manually parse unary prefixes.
1818 bool IsNeg = false;
1819 if (getLexer().is(AsmToken::Minus)) {
1820 Lex();
1821 IsNeg = true;
1822 } else if (getLexer().is(AsmToken::Plus))
1823 Lex();
1824
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001825 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001826 getLexer().isNot(AsmToken::Real) &&
1827 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001828 return TokError("unexpected token in directive");
1829
1830 // Convert to an APFloat.
1831 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001832 StringRef IDVal = getTok().getString();
1833 if (getLexer().is(AsmToken::Identifier)) {
1834 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1835 Value = APFloat::getInf(Semantics);
1836 else if (!IDVal.compare_lower("nan"))
1837 Value = APFloat::getNaN(Semantics, false, ~0);
1838 else
1839 return TokError("invalid floating point literal");
1840 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001841 APFloat::opInvalidOp)
1842 return TokError("invalid floating point literal");
1843 if (IsNeg)
1844 Value.changeSign();
1845
1846 // Consume the numeric token.
1847 Lex();
1848
1849 // Emit the value as an integer.
1850 APInt AsInt = Value.bitcastToAPInt();
1851 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1852 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1853
1854 if (getLexer().is(AsmToken::EndOfStatement))
1855 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001856
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001857 if (getLexer().isNot(AsmToken::Comma))
1858 return TokError("unexpected token in directive");
1859 Lex();
1860 }
1861 }
1862
1863 Lex();
1864 return false;
1865}
1866
Daniel Dunbara0d14262009-06-24 23:30:00 +00001867/// ParseDirectiveSpace
1868/// ::= .space expression [ , expression ]
1869bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001870 CheckForValidSection();
1871
Daniel Dunbara0d14262009-06-24 23:30:00 +00001872 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001873 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001874 return true;
1875
1876 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001877 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1878 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001879 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001880 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001881
Daniel Dunbar475839e2009-06-29 20:37:27 +00001882 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001883 return true;
1884
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001885 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001886 return TokError("unexpected token in '.space' directive");
1887 }
1888
Sean Callanan79ed1a82010-01-19 20:22:31 +00001889 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001890
1891 if (NumBytes <= 0)
1892 return TokError("invalid number of bytes in '.space' directive");
1893
1894 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001895 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001896
1897 return false;
1898}
1899
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001900/// ParseDirectiveZero
1901/// ::= .zero expression
1902bool AsmParser::ParseDirectiveZero() {
1903 CheckForValidSection();
1904
1905 int64_t NumBytes;
1906 if (ParseAbsoluteExpression(NumBytes))
1907 return true;
1908
Rafael Espindolae452b172010-10-05 19:42:57 +00001909 int64_t Val = 0;
1910 if (getLexer().is(AsmToken::Comma)) {
1911 Lex();
1912 if (ParseAbsoluteExpression(Val))
1913 return true;
1914 }
1915
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001916 if (getLexer().isNot(AsmToken::EndOfStatement))
1917 return TokError("unexpected token in '.zero' directive");
1918
1919 Lex();
1920
Rafael Espindolae452b172010-10-05 19:42:57 +00001921 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001922
1923 return false;
1924}
1925
Daniel Dunbara0d14262009-06-24 23:30:00 +00001926/// ParseDirectiveFill
1927/// ::= .fill expression , expression , expression
1928bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001929 CheckForValidSection();
1930
Daniel Dunbara0d14262009-06-24 23:30:00 +00001931 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001932 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001933 return true;
1934
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001935 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001936 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001937 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001938
Daniel Dunbara0d14262009-06-24 23:30:00 +00001939 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001940 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001941 return true;
1942
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001943 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001944 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001945 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001946
Daniel Dunbara0d14262009-06-24 23:30:00 +00001947 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001948 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001949 return true;
1950
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001951 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001952 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001953
Sean Callanan79ed1a82010-01-19 20:22:31 +00001954 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001955
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001956 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1957 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001958
1959 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001960 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001961
1962 return false;
1963}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001964
1965/// ParseDirectiveOrg
1966/// ::= .org expression [ , expression ]
1967bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001968 CheckForValidSection();
1969
Daniel Dunbar821e3332009-08-31 08:09:28 +00001970 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001971 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001972 return true;
1973
1974 // Parse optional fill expression.
1975 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001976 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1977 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001978 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001979 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001980
Daniel Dunbar475839e2009-06-29 20:37:27 +00001981 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001982 return true;
1983
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001984 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001985 return TokError("unexpected token in '.org' directive");
1986 }
1987
Sean Callanan79ed1a82010-01-19 20:22:31 +00001988 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001989
1990 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1991 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001992 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001993
1994 return false;
1995}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001996
1997/// ParseDirectiveAlign
1998/// ::= {.align, ...} expression [ , expression [ , expression ]]
1999bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002000 CheckForValidSection();
2001
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002002 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002003 int64_t Alignment;
2004 if (ParseAbsoluteExpression(Alignment))
2005 return true;
2006
2007 SMLoc MaxBytesLoc;
2008 bool HasFillExpr = false;
2009 int64_t FillExpr = 0;
2010 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002011 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2012 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002013 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002014 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002015
2016 // The fill expression can be omitted while specifying a maximum number of
2017 // alignment bytes, e.g:
2018 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002019 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002020 HasFillExpr = true;
2021 if (ParseAbsoluteExpression(FillExpr))
2022 return true;
2023 }
2024
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002025 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2026 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002027 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002028 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002029
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002030 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002031 if (ParseAbsoluteExpression(MaxBytesToFill))
2032 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002033
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002034 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002035 return TokError("unexpected token in directive");
2036 }
2037 }
2038
Sean Callanan79ed1a82010-01-19 20:22:31 +00002039 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002040
Daniel Dunbar648ac512010-05-17 21:54:30 +00002041 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002042 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002043
2044 // Compute alignment in bytes.
2045 if (IsPow2) {
2046 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002047 if (Alignment >= 32) {
2048 Error(AlignmentLoc, "invalid alignment value");
2049 Alignment = 31;
2050 }
2051
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002052 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002053 }
2054
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002055 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002056 if (MaxBytesLoc.isValid()) {
2057 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002058 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2059 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002060 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002061 }
2062
2063 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002064 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2065 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002066 MaxBytesToFill = 0;
2067 }
2068 }
2069
Daniel Dunbar648ac512010-05-17 21:54:30 +00002070 // Check whether we should use optimal code alignment for this .align
2071 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002072 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002073 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2074 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002075 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002076 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002077 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002078 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2079 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002080 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002081
2082 return false;
2083}
2084
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002085/// ParseDirectiveSymbolAttribute
2086/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002087bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002088 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002089 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002090 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002091 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002092
2093 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002094 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002095
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002096 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002097
Jim Grosbach10ec6502011-09-15 17:56:49 +00002098 // Assembler local symbols don't make any sense here. Complain loudly.
2099 if (Sym->isTemporary())
2100 return Error(Loc, "non-local symbol required in directive");
2101
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002102 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002103
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002104 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002105 break;
2106
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002107 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002108 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002109 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002110 }
2111 }
2112
Sean Callanan79ed1a82010-01-19 20:22:31 +00002113 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002114 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002115}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002116
2117/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002118/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2119bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002120 CheckForValidSection();
2121
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002122 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002123 StringRef Name;
2124 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002125 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002126
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002127 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002128 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002129
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002130 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002131 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002132 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002133
2134 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002135 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002136 if (ParseAbsoluteExpression(Size))
2137 return true;
2138
2139 int64_t Pow2Alignment = 0;
2140 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002141 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002142 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002143 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002144 if (ParseAbsoluteExpression(Pow2Alignment))
2145 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002146
Chris Lattner258281d2010-01-19 06:22:22 +00002147 // If this target takes alignments in bytes (not log) validate and convert.
2148 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2149 if (!isPowerOf2_64(Pow2Alignment))
2150 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2151 Pow2Alignment = Log2_64(Pow2Alignment);
2152 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002153 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002154
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002155 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002156 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002157
Sean Callanan79ed1a82010-01-19 20:22:31 +00002158 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002159
Chris Lattner1fc3d752009-07-09 17:25:12 +00002160 // NOTE: a size of zero for a .comm should create a undefined symbol
2161 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002162 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002163 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2164 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002165
Eric Christopherc260a3e2010-05-14 01:38:54 +00002166 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002167 // may internally end up wanting an alignment in bytes.
2168 // FIXME: Diagnose overflow.
2169 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002170 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2171 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002172
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002173 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002174 return Error(IDLoc, "invalid symbol redefinition");
2175
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002176 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002177 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002178 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002179 getStreamer().EmitZerofill(Ctx.getMachOSection(
2180 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2181 0, SectionKind::getBSS()),
2182 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002183 return false;
2184 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002185
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002186 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002187 return false;
2188}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002189
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002190/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002191/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002192bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002193 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002194 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002195
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002196 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002197 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002198 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002199
Sean Callanan79ed1a82010-01-19 20:22:31 +00002200 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002201
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002202 if (Str.empty())
2203 Error(Loc, ".abort detected. Assembly stopping.");
2204 else
2205 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002206 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002207
2208 return false;
2209}
Kevin Enderby71148242009-07-14 21:35:03 +00002210
Kevin Enderby1f049b22009-07-14 23:21:55 +00002211/// ParseDirectiveInclude
2212/// ::= .include "filename"
2213bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002214 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002215 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002216
Sean Callanan18b83232010-01-19 21:44:56 +00002217 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002218 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002219 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002220
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002221 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002222 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002223
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002224 // Strip the quotes.
2225 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002226
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002227 // Attempt to switch the lexer to the included file before consuming the end
2228 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002229 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002230 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002231 return true;
2232 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002233
2234 return false;
2235}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002236
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002237/// ParseDirectiveIncbin
2238/// ::= .incbin "filename"
2239bool AsmParser::ParseDirectiveIncbin() {
2240 if (getLexer().isNot(AsmToken::String))
2241 return TokError("expected string in '.incbin' directive");
2242
2243 std::string Filename = getTok().getString();
2244 SMLoc IncbinLoc = getLexer().getLoc();
2245 Lex();
2246
2247 if (getLexer().isNot(AsmToken::EndOfStatement))
2248 return TokError("unexpected token in '.incbin' directive");
2249
2250 // Strip the quotes.
2251 Filename = Filename.substr(1, Filename.size()-2);
2252
2253 // Attempt to process the included file.
2254 if (ProcessIncbinFile(Filename)) {
2255 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2256 return true;
2257 }
2258
2259 return false;
2260}
2261
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002262/// ParseDirectiveIf
2263/// ::= .if expression
2264bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002265 TheCondStack.push_back(TheCondState);
2266 TheCondState.TheCond = AsmCond::IfCond;
2267 if(TheCondState.Ignore) {
2268 EatToEndOfStatement();
2269 }
2270 else {
2271 int64_t ExprValue;
2272 if (ParseAbsoluteExpression(ExprValue))
2273 return true;
2274
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002275 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002276 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002277
Sean Callanan79ed1a82010-01-19 20:22:31 +00002278 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002279
2280 TheCondState.CondMet = ExprValue;
2281 TheCondState.Ignore = !TheCondState.CondMet;
2282 }
2283
2284 return false;
2285}
2286
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002287bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2288 StringRef Name;
2289 TheCondStack.push_back(TheCondState);
2290 TheCondState.TheCond = AsmCond::IfCond;
2291
2292 if (TheCondState.Ignore) {
2293 EatToEndOfStatement();
2294 } else {
2295 if (ParseIdentifier(Name))
2296 return TokError("expected identifier after '.ifdef'");
2297
2298 Lex();
2299
2300 MCSymbol *Sym = getContext().LookupSymbol(Name);
2301
2302 if (expect_defined)
2303 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2304 else
2305 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2306 TheCondState.Ignore = !TheCondState.CondMet;
2307 }
2308
2309 return false;
2310}
2311
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002312/// ParseDirectiveElseIf
2313/// ::= .elseif expression
2314bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2315 if (TheCondState.TheCond != AsmCond::IfCond &&
2316 TheCondState.TheCond != AsmCond::ElseIfCond)
2317 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2318 " an .elseif");
2319 TheCondState.TheCond = AsmCond::ElseIfCond;
2320
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002321 bool LastIgnoreState = false;
2322 if (!TheCondStack.empty())
2323 LastIgnoreState = TheCondStack.back().Ignore;
2324 if (LastIgnoreState || TheCondState.CondMet) {
2325 TheCondState.Ignore = true;
2326 EatToEndOfStatement();
2327 }
2328 else {
2329 int64_t ExprValue;
2330 if (ParseAbsoluteExpression(ExprValue))
2331 return true;
2332
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002333 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002334 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002335
Sean Callanan79ed1a82010-01-19 20:22:31 +00002336 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002337 TheCondState.CondMet = ExprValue;
2338 TheCondState.Ignore = !TheCondState.CondMet;
2339 }
2340
2341 return false;
2342}
2343
2344/// ParseDirectiveElse
2345/// ::= .else
2346bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002347 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002348 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002349
Sean Callanan79ed1a82010-01-19 20:22:31 +00002350 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002351
2352 if (TheCondState.TheCond != AsmCond::IfCond &&
2353 TheCondState.TheCond != AsmCond::ElseIfCond)
2354 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2355 ".elseif");
2356 TheCondState.TheCond = AsmCond::ElseCond;
2357 bool LastIgnoreState = false;
2358 if (!TheCondStack.empty())
2359 LastIgnoreState = TheCondStack.back().Ignore;
2360 if (LastIgnoreState || TheCondState.CondMet)
2361 TheCondState.Ignore = true;
2362 else
2363 TheCondState.Ignore = false;
2364
2365 return false;
2366}
2367
2368/// ParseDirectiveEndIf
2369/// ::= .endif
2370bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002371 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002372 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002373
Sean Callanan79ed1a82010-01-19 20:22:31 +00002374 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002375
2376 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2377 TheCondStack.empty())
2378 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2379 ".else");
2380 if (!TheCondStack.empty()) {
2381 TheCondState = TheCondStack.back();
2382 TheCondStack.pop_back();
2383 }
2384
2385 return false;
2386}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002387
2388/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002389/// ::= .file [number] filename
2390/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002391bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002392 // FIXME: I'm not sure what this is.
2393 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002394 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002395 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002396 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002397 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002398
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002399 if (FileNumber < 1)
2400 return TokError("file number less than one");
2401 }
2402
Daniel Dunbareceec052010-07-12 17:45:27 +00002403 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002404 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002405
Nick Lewycky44d798d2011-10-17 23:05:28 +00002406 // Usually the directory and filename together, otherwise just the directory.
2407 StringRef Path = getTok().getString();
2408 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002409 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002410
Nick Lewycky44d798d2011-10-17 23:05:28 +00002411 StringRef Directory;
2412 StringRef Filename;
2413 if (getLexer().is(AsmToken::String)) {
2414 if (FileNumber == -1)
2415 return TokError("explicit path specified, but no file number");
2416 Filename = getTok().getString();
2417 Filename = Filename.substr(1, Filename.size()-2);
2418 Directory = Path;
2419 Lex();
2420 } else {
2421 Filename = Path;
2422 }
2423
Daniel Dunbareceec052010-07-12 17:45:27 +00002424 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002425 return TokError("unexpected token in '.file' directive");
2426
Kevin Enderby613b7572011-11-01 22:27:22 +00002427 if (getContext().getGenDwarfForAssembly() == true)
2428 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2429 "used to generate dwarf debug info for assembly code");
2430
Chris Lattnerd32e8032010-01-25 19:02:58 +00002431 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002432 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002433 else {
Nick Lewycky44d798d2011-10-17 23:05:28 +00002434 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002435 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002436 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002437
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002438 return false;
2439}
2440
2441/// ParseDirectiveLine
2442/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002443bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002444 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2445 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002446 return TokError("unexpected token in '.line' directive");
2447
Sean Callanan18b83232010-01-19 21:44:56 +00002448 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002449 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002450 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002451
2452 // FIXME: Do something with the .line.
2453 }
2454
Daniel Dunbareceec052010-07-12 17:45:27 +00002455 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002456 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002457
2458 return false;
2459}
2460
2461
2462/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002463/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002464/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2465/// The first number is a file number, must have been previously assigned with
2466/// a .file directive, the second number is the line number and optionally the
2467/// third number is a column position (zero if not specified). The remaining
2468/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002469bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002470
Daniel Dunbareceec052010-07-12 17:45:27 +00002471 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002472 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002473 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002474 if (FileNumber < 1)
2475 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002476 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002477 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002478 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002479
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002480 int64_t LineNumber = 0;
2481 if (getLexer().is(AsmToken::Integer)) {
2482 LineNumber = getTok().getIntVal();
2483 if (LineNumber < 1)
2484 return TokError("line number less than one in '.loc' directive");
2485 Lex();
2486 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002487
2488 int64_t ColumnPos = 0;
2489 if (getLexer().is(AsmToken::Integer)) {
2490 ColumnPos = getTok().getIntVal();
2491 if (ColumnPos < 0)
2492 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002493 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002494 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002495
Kevin Enderbyc0957932010-09-30 16:52:03 +00002496 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002497 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002498 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002499 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2500 for (;;) {
2501 if (getLexer().is(AsmToken::EndOfStatement))
2502 break;
2503
2504 StringRef Name;
2505 SMLoc Loc = getTok().getLoc();
2506 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002507 return TokError("unexpected token in '.loc' directive");
2508
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002509 if (Name == "basic_block")
2510 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2511 else if (Name == "prologue_end")
2512 Flags |= DWARF2_FLAG_PROLOGUE_END;
2513 else if (Name == "epilogue_begin")
2514 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2515 else if (Name == "is_stmt") {
2516 SMLoc Loc = getTok().getLoc();
2517 const MCExpr *Value;
2518 if (getParser().ParseExpression(Value))
2519 return true;
2520 // The expression must be the constant 0 or 1.
2521 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2522 int Value = MCE->getValue();
2523 if (Value == 0)
2524 Flags &= ~DWARF2_FLAG_IS_STMT;
2525 else if (Value == 1)
2526 Flags |= DWARF2_FLAG_IS_STMT;
2527 else
2528 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002529 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002530 else {
2531 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2532 }
2533 }
2534 else if (Name == "isa") {
2535 SMLoc Loc = getTok().getLoc();
2536 const MCExpr *Value;
2537 if (getParser().ParseExpression(Value))
2538 return true;
2539 // The expression must be a constant greater or equal to 0.
2540 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2541 int Value = MCE->getValue();
2542 if (Value < 0)
2543 return Error(Loc, "isa number less than zero");
2544 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002545 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002546 else {
2547 return Error(Loc, "isa number not a constant value");
2548 }
2549 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002550 else if (Name == "discriminator") {
2551 if (getParser().ParseAbsoluteExpression(Discriminator))
2552 return true;
2553 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002554 else {
2555 return Error(Loc, "unknown sub-directive in '.loc' directive");
2556 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002557
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002558 if (getLexer().is(AsmToken::EndOfStatement))
2559 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002560 }
2561 }
2562
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002563 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002564 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002565
2566 return false;
2567}
2568
Daniel Dunbar138abae2010-10-16 04:56:42 +00002569/// ParseDirectiveStabs
2570/// ::= .stabs string, number, number, number
2571bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2572 SMLoc DirectiveLoc) {
2573 return TokError("unsupported directive '" + Directive + "'");
2574}
2575
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002576/// ParseDirectiveCFISections
2577/// ::= .cfi_sections section [, section]
2578bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2579 SMLoc DirectiveLoc) {
2580 StringRef Name;
2581 bool EH = false;
2582 bool Debug = false;
2583
2584 if (getParser().ParseIdentifier(Name))
2585 return TokError("Expected an identifier");
2586
2587 if (Name == ".eh_frame")
2588 EH = true;
2589 else if (Name == ".debug_frame")
2590 Debug = true;
2591
2592 if (getLexer().is(AsmToken::Comma)) {
2593 Lex();
2594
2595 if (getParser().ParseIdentifier(Name))
2596 return TokError("Expected an identifier");
2597
2598 if (Name == ".eh_frame")
2599 EH = true;
2600 else if (Name == ".debug_frame")
2601 Debug = true;
2602 }
2603
2604 getStreamer().EmitCFISections(EH, Debug);
2605
2606 return false;
2607}
2608
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002609/// ParseDirectiveCFIStartProc
2610/// ::= .cfi_startproc
2611bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2612 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002613 getStreamer().EmitCFIStartProc();
2614 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002615}
2616
2617/// ParseDirectiveCFIEndProc
2618/// ::= .cfi_endproc
2619bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002620 getStreamer().EmitCFIEndProc();
2621 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002622}
2623
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002624/// ParseRegisterOrRegisterNumber - parse register name or number.
2625bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2626 SMLoc DirectiveLoc) {
2627 unsigned RegNo;
2628
Jim Grosbach6f888a82011-06-02 17:14:04 +00002629 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002630 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2631 DirectiveLoc))
2632 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002633 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002634 } else
2635 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002636
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002637 return false;
2638}
2639
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002640/// ParseDirectiveCFIDefCfa
2641/// ::= .cfi_def_cfa register, offset
2642bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2643 SMLoc DirectiveLoc) {
2644 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002645 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002646 return true;
2647
2648 if (getLexer().isNot(AsmToken::Comma))
2649 return TokError("unexpected token in directive");
2650 Lex();
2651
2652 int64_t Offset = 0;
2653 if (getParser().ParseAbsoluteExpression(Offset))
2654 return true;
2655
Rafael Espindola066c2f42011-04-12 23:59:07 +00002656 getStreamer().EmitCFIDefCfa(Register, Offset);
2657 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002658}
2659
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002660/// ParseDirectiveCFIDefCfaOffset
2661/// ::= .cfi_def_cfa_offset offset
2662bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2663 SMLoc DirectiveLoc) {
2664 int64_t Offset = 0;
2665 if (getParser().ParseAbsoluteExpression(Offset))
2666 return true;
2667
Rafael Espindola066c2f42011-04-12 23:59:07 +00002668 getStreamer().EmitCFIDefCfaOffset(Offset);
2669 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002670}
2671
2672/// ParseDirectiveCFIAdjustCfaOffset
2673/// ::= .cfi_adjust_cfa_offset adjustment
2674bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2675 SMLoc DirectiveLoc) {
2676 int64_t Adjustment = 0;
2677 if (getParser().ParseAbsoluteExpression(Adjustment))
2678 return true;
2679
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002680 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2681 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002682}
2683
2684/// ParseDirectiveCFIDefCfaRegister
2685/// ::= .cfi_def_cfa_register register
2686bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2687 SMLoc DirectiveLoc) {
2688 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002689 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002690 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002691
Rafael Espindola066c2f42011-04-12 23:59:07 +00002692 getStreamer().EmitCFIDefCfaRegister(Register);
2693 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002694}
2695
2696/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002697/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002698bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2699 int64_t Register = 0;
2700 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002701
2702 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002703 return true;
2704
2705 if (getLexer().isNot(AsmToken::Comma))
2706 return TokError("unexpected token in directive");
2707 Lex();
2708
2709 if (getParser().ParseAbsoluteExpression(Offset))
2710 return true;
2711
Rafael Espindola066c2f42011-04-12 23:59:07 +00002712 getStreamer().EmitCFIOffset(Register, Offset);
2713 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002714}
2715
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002716/// ParseDirectiveCFIRelOffset
2717/// ::= .cfi_rel_offset register, offset
2718bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2719 SMLoc DirectiveLoc) {
2720 int64_t Register = 0;
2721
2722 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2723 return true;
2724
2725 if (getLexer().isNot(AsmToken::Comma))
2726 return TokError("unexpected token in directive");
2727 Lex();
2728
2729 int64_t Offset = 0;
2730 if (getParser().ParseAbsoluteExpression(Offset))
2731 return true;
2732
Rafael Espindola25f492e2011-04-12 16:12:03 +00002733 getStreamer().EmitCFIRelOffset(Register, Offset);
2734 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002735}
2736
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002737static bool isValidEncoding(int64_t Encoding) {
2738 if (Encoding & ~0xff)
2739 return false;
2740
2741 if (Encoding == dwarf::DW_EH_PE_omit)
2742 return true;
2743
2744 const unsigned Format = Encoding & 0xf;
2745 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2746 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2747 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2748 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2749 return false;
2750
Rafael Espindolacaf11582010-12-29 04:31:26 +00002751 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002752 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002753 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002754 return false;
2755
2756 return true;
2757}
2758
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002759/// ParseDirectiveCFIPersonalityOrLsda
2760/// ::= .cfi_personality encoding, [symbol_name]
2761/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002762bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002763 SMLoc DirectiveLoc) {
2764 int64_t Encoding = 0;
2765 if (getParser().ParseAbsoluteExpression(Encoding))
2766 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002767 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002768 return false;
2769
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002770 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002771 return TokError("unsupported encoding.");
2772
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002773 if (getLexer().isNot(AsmToken::Comma))
2774 return TokError("unexpected token in directive");
2775 Lex();
2776
2777 StringRef Name;
2778 if (getParser().ParseIdentifier(Name))
2779 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002780
2781 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2782
2783 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002784 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002785 else {
2786 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002787 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002788 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002789 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002790}
2791
Rafael Espindolafe024d02010-12-28 18:36:23 +00002792/// ParseDirectiveCFIRememberState
2793/// ::= .cfi_remember_state
2794bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2795 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002796 getStreamer().EmitCFIRememberState();
2797 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002798}
2799
2800/// ParseDirectiveCFIRestoreState
2801/// ::= .cfi_remember_state
2802bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2803 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002804 getStreamer().EmitCFIRestoreState();
2805 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002806}
2807
Rafael Espindolac5754392011-04-12 15:31:05 +00002808/// ParseDirectiveCFISameValue
2809/// ::= .cfi_same_value register
2810bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2811 SMLoc DirectiveLoc) {
2812 int64_t Register = 0;
2813
2814 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2815 return true;
2816
2817 getStreamer().EmitCFISameValue(Register);
2818
2819 return false;
2820}
2821
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00002822/// ParseDirectiveCFIRestore
2823/// ::= .cfi_restore register
2824bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
2825 SMLoc DirectiveLoc) {
2826 int64_t Register = 0;
2827 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2828 return true;
2829
2830 getStreamer().EmitCFIRestore(Register);
2831
2832 return false;
2833}
2834
Rafael Espindola6f0b1812011-12-29 20:24:47 +00002835/// ParseDirectiveCFIEscape
2836/// ::= .cfi_escape expression[,...]
2837bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
2838 SMLoc DirectiveLoc) {
2839 std::string Values;
2840 int64_t CurrValue;
2841 if (getParser().ParseAbsoluteExpression(CurrValue))
2842 return true;
2843
2844 Values.push_back((uint8_t)CurrValue);
2845
2846 while (getLexer().is(AsmToken::Comma)) {
2847 Lex();
2848
2849 if (getParser().ParseAbsoluteExpression(CurrValue))
2850 return true;
2851
2852 Values.push_back((uint8_t)CurrValue);
2853 }
2854
2855 getStreamer().EmitCFIEscape(Values);
2856 return false;
2857}
2858
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002859/// ParseDirectiveMacrosOnOff
2860/// ::= .macros_on
2861/// ::= .macros_off
2862bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2863 SMLoc DirectiveLoc) {
2864 if (getLexer().isNot(AsmToken::EndOfStatement))
2865 return Error(getLexer().getLoc(),
2866 "unexpected token in '" + Directive + "' directive");
2867
2868 getParser().MacrosEnabled = Directive == ".macros_on";
2869
2870 return false;
2871}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002872
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002873/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002874/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002875bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2876 SMLoc DirectiveLoc) {
2877 StringRef Name;
2878 if (getParser().ParseIdentifier(Name))
2879 return TokError("expected identifier in directive");
2880
Rafael Espindola65366442011-06-05 02:43:45 +00002881 std::vector<StringRef> Parameters;
2882 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2883 for(;;) {
2884 StringRef Parameter;
2885 if (getParser().ParseIdentifier(Parameter))
2886 return TokError("expected identifier in directive");
2887 Parameters.push_back(Parameter);
2888
2889 if (getLexer().isNot(AsmToken::Comma))
2890 break;
2891 Lex();
2892 }
2893 }
2894
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002895 if (getLexer().isNot(AsmToken::EndOfStatement))
2896 return TokError("unexpected token in '.macro' directive");
2897
2898 // Eat the end of statement.
2899 Lex();
2900
2901 AsmToken EndToken, StartToken = getTok();
2902
2903 // Lex the macro definition.
2904 for (;;) {
2905 // Check whether we have reached the end of the file.
2906 if (getLexer().is(AsmToken::Eof))
2907 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2908
2909 // Otherwise, check whether we have reach the .endmacro.
2910 if (getLexer().is(AsmToken::Identifier) &&
2911 (getTok().getIdentifier() == ".endm" ||
2912 getTok().getIdentifier() == ".endmacro")) {
2913 EndToken = getTok();
2914 Lex();
2915 if (getLexer().isNot(AsmToken::EndOfStatement))
2916 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2917 "' directive");
2918 break;
2919 }
2920
2921 // Otherwise, scan til the end of the statement.
2922 getParser().EatToEndOfStatement();
2923 }
2924
2925 if (getParser().MacroMap.lookup(Name)) {
2926 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2927 }
2928
2929 const char *BodyStart = StartToken.getLoc().getPointer();
2930 const char *BodyEnd = EndToken.getLoc().getPointer();
2931 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002932 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002933 return false;
2934}
2935
2936/// ParseDirectiveEndMacro
2937/// ::= .endm
2938/// ::= .endmacro
2939bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2940 SMLoc DirectiveLoc) {
2941 if (getLexer().isNot(AsmToken::EndOfStatement))
2942 return TokError("unexpected token in '" + Directive + "' directive");
2943
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002944 // If we are inside a macro instantiation, terminate the current
2945 // instantiation.
2946 if (!getParser().ActiveMacros.empty()) {
2947 getParser().HandleMacroExit();
2948 return false;
2949 }
2950
2951 // Otherwise, this .endmacro is a stray entry in the file; well formed
2952 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002953 return TokError("unexpected '" + Directive + "' in file, "
2954 "no current macro definition");
2955}
2956
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002957bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002958 getParser().CheckForValidSection();
2959
2960 const MCExpr *Value;
2961
2962 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002963 return true;
2964
2965 if (getLexer().isNot(AsmToken::EndOfStatement))
2966 return TokError("unexpected token in directive");
2967
2968 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002969 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002970 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002971 getStreamer().EmitULEB128Value(Value);
2972
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002973 return false;
2974}
2975
2976
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002977/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002978MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002979 MCContext &C, MCStreamer &Out,
2980 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002981 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002982}