blob: 1af7cd9ee0d9694972ffd29bb8f494e503bf184b [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 Espindola16d7d432012-01-23 21:51:52 +0000309 AddDirectiveHandler<
310 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000311
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000312 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000313 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
314 ".macros_on");
315 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
316 ".macros_off");
317 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
318 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
319 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000320
321 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
322 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000323 }
324
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000325 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
326
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000327 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
328 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
329 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000330 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000331 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000332 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
333 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000334 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000335 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000336 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000337 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
338 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000339 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000340 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000341 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
342 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000343 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000344 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000345 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000346 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000347
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000348 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000349 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
350 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000351
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000352 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000353};
354
355}
356
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000357namespace llvm {
358
359extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000360extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000361extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000362
363}
364
Chris Lattneraaec2052010-01-19 19:46:13 +0000365enum { DEFAULT_ADDRSPACE = 0 };
366
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000367AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000368 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000369 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000370 GenericParser(new GenericAsmParser), PlatformParser(0),
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000371 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000372 // Save the old handler.
373 SavedDiagHandler = SrcMgr.getDiagHandler();
374 SavedDiagContext = SrcMgr.getDiagContext();
375 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000376 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000377 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000378
379 // Initialize the generic parser.
380 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000381
382 // Initialize the platform / file format parser.
383 //
384 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
385 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000386 if (_MAI.hasMicrosoftFastStdCallMangling()) {
387 PlatformParser = createCOFFAsmParser();
388 PlatformParser->Initialize(*this);
389 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000390 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000391 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000392 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000393 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000394 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000395 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000396}
397
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000398AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000399 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
400
401 // Destroy any macros.
402 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
403 ie = MacroMap.end(); it != ie; ++it)
404 delete it->getValue();
405
Daniel Dunbare4749702010-07-12 18:12:02 +0000406 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000407 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000408}
409
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000410void AsmParser::PrintMacroInstantiations() {
411 // Print the active macro instantiation stack.
412 for (std::vector<MacroInstantiation*>::const_reverse_iterator
413 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000414 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
415 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000416}
417
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000418bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000419 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000420 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000421 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000422 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000423 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000424}
425
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000426bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000427 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000428 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000429 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000430 return true;
431}
432
Sean Callananfd0b0282010-01-21 00:19:58 +0000433bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000434 std::string IncludedFile;
435 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000436 if (NewBuf == -1)
437 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000438
Sean Callananfd0b0282010-01-21 00:19:58 +0000439 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000440
Sean Callananfd0b0282010-01-21 00:19:58 +0000441 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000442
Sean Callananfd0b0282010-01-21 00:19:58 +0000443 return false;
444}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000445
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000446/// Process the specified .incbin file by seaching for it in the include paths
447/// then just emiting the byte contents of the file to the streamer. This
448/// returns true on failure.
449bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
450 std::string IncludedFile;
451 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
452 if (NewBuf == -1)
453 return true;
454
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000455 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000456 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
457 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000458 return false;
459}
460
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000461void AsmParser::JumpToLoc(SMLoc Loc) {
462 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
463 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
464}
465
Sean Callananfd0b0282010-01-21 00:19:58 +0000466const AsmToken &AsmParser::Lex() {
467 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000468
Sean Callananfd0b0282010-01-21 00:19:58 +0000469 if (tok->is(AsmToken::Eof)) {
470 // If this is the end of an included file, pop the parent file off the
471 // include stack.
472 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
473 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000474 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000475 tok = &Lexer.Lex();
476 }
477 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000478
Sean Callananfd0b0282010-01-21 00:19:58 +0000479 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000480 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000481
Sean Callananfd0b0282010-01-21 00:19:58 +0000482 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000483}
484
Chris Lattner79180e22010-04-05 23:15:42 +0000485bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000486 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000487 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000488 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000489
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000490 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000491 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000492
493 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000494 AsmCond StartingCondState = TheCondState;
495
Kevin Enderby613b7572011-11-01 22:27:22 +0000496 // If we are generating dwarf for assembly source files save the initial text
497 // section and generate a .file directive.
498 if (getContext().getGenDwarfForAssembly()) {
499 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000500 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
501 getStreamer().EmitLabel(SectionStartSym);
502 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000503 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
504 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
505 }
506
Chris Lattnerb717fb02009-07-02 21:53:43 +0000507 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000508 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000509 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000510
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000511 // We had an error, validate that one was emitted and recover by skipping to
512 // the next line.
513 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000514 EatToEndOfStatement();
515 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000516
517 if (TheCondState.TheCond != StartingCondState.TheCond ||
518 TheCondState.Ignore != StartingCondState.Ignore)
519 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000520
521 // Check to see there are no empty DwarfFile slots.
522 const std::vector<MCDwarfFile *> &MCDwarfFiles =
523 getContext().getMCDwarfFiles();
524 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000525 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000526 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000527 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000528
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000529 // Check to see that all assembler local symbols were actually defined.
530 // Targets that don't do subsections via symbols may not want this, though,
531 // so conservatively exclude them. Only do this if we're finalizing, though,
532 // as otherwise we won't necessarilly have seen everything yet.
533 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
534 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
535 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
536 e = Symbols.end();
537 i != e; ++i) {
538 MCSymbol *Sym = i->getValue();
539 // Variable symbols may not be marked as defined, so check those
540 // explicitly. If we know it's a variable, we have a definition for
541 // the purposes of this check.
542 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
543 // FIXME: We would really like to refer back to where the symbol was
544 // first referenced for a source location. We need to add something
545 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000546 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
547 "assembler local symbol '" + Sym->getName() +
548 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000549 }
550 }
551
552
Chris Lattner79180e22010-04-05 23:15:42 +0000553 // Finalize the output stream if there are no errors and if the client wants
554 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000555 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000556 Out.Finish();
557
Chris Lattnerb717fb02009-07-02 21:53:43 +0000558 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000559}
560
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000561void AsmParser::CheckForValidSection() {
562 if (!getStreamer().getCurrentSection()) {
563 TokError("expected section directive before assembly directive");
564 Out.SwitchSection(Ctx.getMachOSection(
565 "__TEXT", "__text",
566 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
567 0, SectionKind::getText()));
568 }
569}
570
Chris Lattner2cf5f142009-06-22 01:29:09 +0000571/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
572void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000573 while (Lexer.isNot(AsmToken::EndOfStatement) &&
574 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000575 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000576
Chris Lattner2cf5f142009-06-22 01:29:09 +0000577 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000578 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000579 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000580}
581
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000582StringRef AsmParser::ParseStringToEndOfStatement() {
583 const char *Start = getTok().getLoc().getPointer();
584
585 while (Lexer.isNot(AsmToken::EndOfStatement) &&
586 Lexer.isNot(AsmToken::Eof))
587 Lex();
588
589 const char *End = getTok().getLoc().getPointer();
590 return StringRef(Start, End - Start);
591}
Chris Lattnerc4193832009-06-22 05:51:26 +0000592
Chris Lattner74ec1a32009-06-22 06:32:03 +0000593/// ParseParenExpr - Parse a paren expression and return it.
594/// NOTE: This assumes the leading '(' has already been consumed.
595///
596/// parenexpr ::= expr)
597///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000598bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000599 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000600 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000601 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000602 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000603 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000604 return false;
605}
Chris Lattnerc4193832009-06-22 05:51:26 +0000606
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000607/// ParseBracketExpr - Parse a bracket expression and return it.
608/// NOTE: This assumes the leading '[' has already been consumed.
609///
610/// bracketexpr ::= expr]
611///
612bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
613 if (ParseExpression(Res)) return true;
614 if (Lexer.isNot(AsmToken::RBrac))
615 return TokError("expected ']' in brackets expression");
616 EndLoc = Lexer.getLoc();
617 Lex();
618 return false;
619}
620
Chris Lattner74ec1a32009-06-22 06:32:03 +0000621/// ParsePrimaryExpr - Parse a primary expression and return it.
622/// primaryexpr ::= (parenexpr
623/// primaryexpr ::= symbol
624/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000625/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000626/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000627bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000628 switch (Lexer.getKind()) {
629 default:
630 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000631 // If we have an error assume that we've already handled it.
632 case AsmToken::Error:
633 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000634 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000635 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000636 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000637 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000638 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000639 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000640 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000641 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000642 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000643 EndLoc = Lexer.getLoc();
644
645 StringRef Identifier;
646 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000647 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000648
Daniel Dunbarfffff912009-10-16 01:34:54 +0000649 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000650 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000651 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000652
653 // Lookup the symbol variant if used.
654 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000655 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000656 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000657 if (Variant == MCSymbolRefExpr::VK_Invalid) {
658 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000659 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000660 }
661 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000662
Daniel Dunbarfffff912009-10-16 01:34:54 +0000663 // If this is an absolute variable reference, substitute it now to preserve
664 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000665 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000666 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000667 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000668
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000669 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000670 return false;
671 }
672
673 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000674 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000675 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000676 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000677 case AsmToken::Integer: {
678 SMLoc Loc = getTok().getLoc();
679 int64_t IntVal = getTok().getIntVal();
680 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000681 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000682 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000683 // Look for 'b' or 'f' following an Integer as a directional label
684 if (Lexer.getKind() == AsmToken::Identifier) {
685 StringRef IDVal = getTok().getString();
686 if (IDVal == "f" || IDVal == "b"){
687 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
688 IDVal == "f" ? 1 : 0);
689 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
690 getContext());
691 if(IDVal == "b" && Sym->isUndefined())
692 return Error(Loc, "invalid reference to undefined symbol");
693 EndLoc = Lexer.getLoc();
694 Lex(); // Eat identifier.
695 }
696 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000697 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000698 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000699 case AsmToken::Real: {
700 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000701 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000702 Res = MCConstantExpr::Create(IntVal, getContext());
703 Lex(); // Eat token.
704 return false;
705 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000706 case AsmToken::Dot: {
707 // This is a '.' reference, which references the current PC. Emit a
708 // temporary label to the streamer and refer to it.
709 MCSymbol *Sym = Ctx.CreateTempSymbol();
710 Out.EmitLabel(Sym);
711 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
712 EndLoc = Lexer.getLoc();
713 Lex(); // Eat identifier.
714 return false;
715 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000716 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000717 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000718 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000719 case AsmToken::LBrac:
720 if (!PlatformParser->HasBracketExpressions())
721 return TokError("brackets expression not supported on this target");
722 Lex(); // Eat the '['.
723 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000724 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000725 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000726 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000727 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000728 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000729 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000730 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000731 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000732 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000733 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000734 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000735 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000736 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000737 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000738 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000739 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000740 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000741 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000742 }
743}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000744
Chris Lattnerb4307b32010-01-15 19:28:38 +0000745bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000746 SMLoc EndLoc;
747 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000748}
749
Daniel Dunbarcceba832010-09-17 02:47:07 +0000750const MCExpr *
751AsmParser::ApplyModifierToExpr(const MCExpr *E,
752 MCSymbolRefExpr::VariantKind Variant) {
753 // Recurse over the given expression, rebuilding it to apply the given variant
754 // if there is exactly one symbol.
755 switch (E->getKind()) {
756 case MCExpr::Target:
757 case MCExpr::Constant:
758 return 0;
759
760 case MCExpr::SymbolRef: {
761 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
762
763 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
764 TokError("invalid variant on expression '" +
765 getTok().getIdentifier() + "' (already modified)");
766 return E;
767 }
768
769 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
770 }
771
772 case MCExpr::Unary: {
773 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
774 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
775 if (!Sub)
776 return 0;
777 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
778 }
779
780 case MCExpr::Binary: {
781 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
782 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
783 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
784
785 if (!LHS && !RHS)
786 return 0;
787
788 if (!LHS) LHS = BE->getLHS();
789 if (!RHS) RHS = BE->getRHS();
790
791 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
792 }
793 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000794
795 assert(0 && "Invalid expression kind!");
796 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000797}
798
Chris Lattner74ec1a32009-06-22 06:32:03 +0000799/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000800///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000801/// expr ::= expr &&,|| expr -> lowest.
802/// expr ::= expr |,^,&,! expr
803/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
804/// expr ::= expr <<,>> expr
805/// expr ::= expr +,- expr
806/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000807/// expr ::= primaryexpr
808///
Chris Lattner54482b42010-01-15 19:39:23 +0000809bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000810 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000811 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000812 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
813 return true;
814
Daniel Dunbarcceba832010-09-17 02:47:07 +0000815 // As a special case, we support 'a op b @ modifier' by rewriting the
816 // expression to include the modifier. This is inefficient, but in general we
817 // expect users to use 'a@modifier op b'.
818 if (Lexer.getKind() == AsmToken::At) {
819 Lex();
820
821 if (Lexer.isNot(AsmToken::Identifier))
822 return TokError("unexpected symbol modifier following '@'");
823
824 MCSymbolRefExpr::VariantKind Variant =
825 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
826 if (Variant == MCSymbolRefExpr::VK_Invalid)
827 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
828
829 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
830 if (!ModifiedRes) {
831 return TokError("invalid modifier '" + getTok().getIdentifier() +
832 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000833 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000834
Daniel Dunbarcceba832010-09-17 02:47:07 +0000835 Res = ModifiedRes;
836 Lex();
837 }
838
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000839 // Try to constant fold it up front, if possible.
840 int64_t Value;
841 if (Res->EvaluateAsAbsolute(Value))
842 Res = MCConstantExpr::Create(Value, getContext());
843
844 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000845}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000846
Chris Lattnerb4307b32010-01-15 19:28:38 +0000847bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000848 Res = 0;
849 return ParseParenExpr(Res, EndLoc) ||
850 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000851}
852
Daniel Dunbar475839e2009-06-29 20:37:27 +0000853bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000854 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000855
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000856 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000857 if (ParseExpression(Expr))
858 return true;
859
Daniel Dunbare00b0112009-10-16 01:57:52 +0000860 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000861 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000862
863 return false;
864}
865
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000866static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000867 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000868 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000869 default:
870 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000871
Jim Grosbachfbe16812011-08-20 16:24:13 +0000872 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000873 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000874 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000875 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000876 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000877 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000878 return 1;
879
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000880
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000881 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000882 //
883 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000884 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000885 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000886 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000887 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000888 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000889 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000890 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000891 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000892 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000893
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000894 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000895 case AsmToken::EqualEqual:
896 Kind = MCBinaryExpr::EQ;
897 return 3;
898 case AsmToken::ExclaimEqual:
899 case AsmToken::LessGreater:
900 Kind = MCBinaryExpr::NE;
901 return 3;
902 case AsmToken::Less:
903 Kind = MCBinaryExpr::LT;
904 return 3;
905 case AsmToken::LessEqual:
906 Kind = MCBinaryExpr::LTE;
907 return 3;
908 case AsmToken::Greater:
909 Kind = MCBinaryExpr::GT;
910 return 3;
911 case AsmToken::GreaterEqual:
912 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000913 return 3;
914
Jim Grosbachfbe16812011-08-20 16:24:13 +0000915 // Intermediate Precedence: <<, >>
916 case AsmToken::LessLess:
917 Kind = MCBinaryExpr::Shl;
918 return 4;
919 case AsmToken::GreaterGreater:
920 Kind = MCBinaryExpr::Shr;
921 return 4;
922
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000923 // High Intermediate Precedence: +, -
924 case AsmToken::Plus:
925 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000926 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000927 case AsmToken::Minus:
928 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000929 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000930
Jim Grosbachfbe16812011-08-20 16:24:13 +0000931 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000932 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000933 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000934 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000935 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000936 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000937 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000938 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000939 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000940 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000941 }
942}
943
944
945/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
946/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000947bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
948 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000949 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000950 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000951 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000952
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000953 // If the next token is lower precedence than we are allowed to eat, return
954 // successfully with what we ate already.
955 if (TokPrec < Precedence)
956 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000957
Sean Callanan79ed1a82010-01-19 20:22:31 +0000958 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000959
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000960 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000961 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000962 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000963
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000964 // If BinOp binds less tightly with RHS than the operator after RHS, let
965 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000966 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000967 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000968 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000969 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000970 }
971
Daniel Dunbar475839e2009-06-29 20:37:27 +0000972 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000973 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000974 }
975}
976
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000977
978
979
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000980/// ParseStatement:
981/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000982/// ::= Label* Directive ...Operands... EndOfStatement
983/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000984bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000985 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000986 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000987 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000988 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000989 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000990
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000991 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000992 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000993 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000994 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000995 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000996 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000997 if (Lexer.is(AsmToken::Hash))
998 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000999
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001000 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001001 if (Lexer.is(AsmToken::Integer)) {
1002 LocalLabelVal = getTok().getIntVal();
1003 if (LocalLabelVal < 0) {
1004 if (!TheCondState.Ignore)
1005 return TokError("unexpected token at start of statement");
1006 IDVal = "";
1007 }
1008 else {
1009 IDVal = getTok().getString();
1010 Lex(); // Consume the integer token to be used as an identifier token.
1011 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001012 if (!TheCondState.Ignore)
1013 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001014 }
1015 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001016
1017 } else if (Lexer.is(AsmToken::Dot)) {
1018 // Treat '.' as a valid identifier in this context.
1019 Lex();
1020 IDVal = ".";
1021
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001022 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001023 if (!TheCondState.Ignore)
1024 return TokError("unexpected token at start of statement");
1025 IDVal = "";
1026 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001027
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001028
Chris Lattner7834fac2010-04-17 18:14:27 +00001029 // Handle conditional assembly here before checking for skipping. We
1030 // have to do this so that .endif isn't skipped in a ".if 0" block for
1031 // example.
1032 if (IDVal == ".if")
1033 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001034 if (IDVal == ".ifdef")
1035 return ParseDirectiveIfdef(IDLoc, true);
1036 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1037 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001038 if (IDVal == ".elseif")
1039 return ParseDirectiveElseIf(IDLoc);
1040 if (IDVal == ".else")
1041 return ParseDirectiveElse(IDLoc);
1042 if (IDVal == ".endif")
1043 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001044
Chris Lattner7834fac2010-04-17 18:14:27 +00001045 // If we are in a ".if 0" block, ignore this statement.
1046 if (TheCondState.Ignore) {
1047 EatToEndOfStatement();
1048 return false;
1049 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001050
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001051 // FIXME: Recurse on local labels?
1052
1053 // See what kind of statement we have.
1054 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001055 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001056 CheckForValidSection();
1057
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001058 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001059 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001060
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001061 // Diagnose attempt to use '.' as a label.
1062 if (IDVal == ".")
1063 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1064
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001065 // Diagnose attempt to use a variable as a label.
1066 //
1067 // FIXME: Diagnostics. Note the location of the definition as a label.
1068 // FIXME: This doesn't diagnose assignment to a symbol which has been
1069 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001070 MCSymbol *Sym;
1071 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001072 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001073 else
1074 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001075 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001076 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001077
Daniel Dunbar959fd882009-08-26 22:13:22 +00001078 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001079 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001080
Kevin Enderby94c2e852011-12-09 18:09:40 +00001081 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001082 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001083 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001084 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1085 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001086
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001087 // Consume any end of statement token, if present, to avoid spurious
1088 // AddBlankLine calls().
1089 if (Lexer.is(AsmToken::EndOfStatement)) {
1090 Lex();
1091 if (Lexer.is(AsmToken::Eof))
1092 return false;
1093 }
1094
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001095 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001096 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001097
Daniel Dunbar3f872332009-07-28 16:08:33 +00001098 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001099 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001100 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001101
Nico Weber4c4c7322011-01-28 03:04:41 +00001102 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001103
1104 default: // Normal instruction or directive.
1105 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001106 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001107
1108 // If macros are enabled, check to see if this is a macro instantiation.
1109 if (MacrosEnabled)
1110 if (const Macro *M = MacroMap.lookup(IDVal))
1111 return HandleMacroEntry(IDVal, IDLoc, M);
1112
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001113 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001114 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001115 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001116 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001117 return ParseDirectiveSet(IDVal, true);
1118 if (IDVal == ".equiv")
1119 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001120
Daniel Dunbara0d14262009-06-24 23:30:00 +00001121 // Data directives
1122
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001123 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001124 return ParseDirectiveAscii(IDVal, false);
1125 if (IDVal == ".asciz" || IDVal == ".string")
1126 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001127
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001128 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001129 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001130 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001131 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001132 if (IDVal == ".value")
1133 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001134 if (IDVal == ".2byte")
1135 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001136 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001137 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001138 if (IDVal == ".int")
1139 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001140 if (IDVal == ".4byte")
1141 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001142 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001143 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001144 if (IDVal == ".8byte")
1145 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001146 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001147 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1148 if (IDVal == ".double")
1149 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001150
Eli Friedman5d68ec22010-07-19 04:17:25 +00001151 if (IDVal == ".align") {
1152 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1153 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1154 }
1155 if (IDVal == ".align32") {
1156 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1157 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1158 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001159 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001160 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001161 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001162 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001163 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001164 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001165 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001166 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001167 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001168 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001169 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001170 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1171
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001172 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001173 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001174
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001175 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001176 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001177 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001178 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001179 if (IDVal == ".zero")
1180 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001181
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001182 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001183
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001184 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001185 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001186 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001187 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001188 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001189 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001190 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001191 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001192 if (IDVal == ".symbol_resolver")
1193 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001194 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001195 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001196 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001197 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001198 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001199 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001200 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001201 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001202 if (IDVal == ".weak_def_can_be_hidden")
1203 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001204
Hans Wennborg5cc64912011-06-18 13:51:54 +00001205 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001206 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001207 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001208 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001209
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001210 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001211 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001212 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001213 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001214 if (IDVal == ".incbin")
1215 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001216
Evan Chengbd27f5a2011-07-27 00:38:12 +00001217 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001218 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001219
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001220 // Look up the handler in the handler table.
1221 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1222 DirectiveMap.lookup(IDVal);
1223 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001224 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001225
Kevin Enderby9c656452009-09-10 20:51:44 +00001226 // Target hook for parsing target specific directives.
1227 if (!getTargetParser().ParseDirective(ID))
1228 return false;
1229
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001230 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001231 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001232 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001233 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001234
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001235 CheckForValidSection();
1236
Chris Lattnera7f13542010-05-19 23:34:33 +00001237 // Canonicalize the opcode to lower case.
1238 SmallString<128> Opcode;
1239 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1240 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001241
Chris Lattner98986712010-01-14 22:21:20 +00001242 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001243 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001244 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001245
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001246 // Dump the parsed representation, if requested.
1247 if (getShowParsedOperands()) {
1248 SmallString<256> Str;
1249 raw_svector_ostream OS(Str);
1250 OS << "parsed instruction: [";
1251 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1252 if (i != 0)
1253 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001254 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001255 }
1256 OS << "]";
1257
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001258 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001259 }
1260
Kevin Enderby613b7572011-11-01 22:27:22 +00001261 // If we are generating dwarf for assembly source files and the current
1262 // section is the initial text section then generate a .loc directive for
1263 // the instruction.
1264 if (!HadError && getContext().getGenDwarfForAssembly() &&
1265 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1266 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1267 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1268 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001269 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001270 StringRef());
1271 }
1272
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001273 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001274 if (!HadError)
1275 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1276 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001277
Chris Lattner98986712010-01-14 22:21:20 +00001278 // Free any parsed operands.
1279 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1280 delete ParsedOperands[i];
1281
Chris Lattnercbf8a982010-09-11 16:18:25 +00001282 // Don't skip the rest of the line, the instruction parser is responsible for
1283 // that.
1284 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001285}
Chris Lattner9a023f72009-06-24 04:43:34 +00001286
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001287/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1288/// since they may not be able to be tokenized to get to the end of line token.
1289void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001290 if (!Lexer.is(AsmToken::EndOfStatement))
1291 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001292 // Eat EOL.
1293 Lex();
1294}
1295
1296/// ParseCppHashLineFilenameComment as this:
1297/// ::= # number "filename"
1298/// or just as a full line comment if it doesn't have a number and a string.
1299bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1300 Lex(); // Eat the hash token.
1301
1302 if (getLexer().isNot(AsmToken::Integer)) {
1303 // Consume the line since in cases it is not a well-formed line directive,
1304 // as if were simply a full line comment.
1305 EatToEndOfLine();
1306 return false;
1307 }
1308
1309 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001310 Lex();
1311
1312 if (getLexer().isNot(AsmToken::String)) {
1313 EatToEndOfLine();
1314 return false;
1315 }
1316
1317 StringRef Filename = getTok().getString();
1318 // Get rid of the enclosing quotes.
1319 Filename = Filename.substr(1, Filename.size()-2);
1320
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001321 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1322 CppHashLoc = L;
1323 CppHashFilename = Filename;
1324 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001325
1326 // Ignore any trailing characters, they're just comment.
1327 EatToEndOfLine();
1328 return false;
1329}
1330
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001331/// DiagHandler - will use the the last parsed cpp hash line filename comment
1332/// for the Filename and LineNo if any in the diagnostic.
1333void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1334 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1335 raw_ostream &OS = errs();
1336
1337 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1338 const SMLoc &DiagLoc = Diag.getLoc();
1339 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1340 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1341
1342 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1343 // before printing the message.
1344 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001345 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001346 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1347 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1348 }
1349
1350 // If we have not parsed a cpp hash line filename comment or the source
1351 // manager changed or buffer changed (like in a nested include) then just
1352 // print the normal diagnostic using its Filename and LineNo.
1353 if (!Parser->CppHashLineNumber ||
1354 &DiagSrcMgr != &Parser->SrcMgr ||
1355 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001356 if (Parser->SavedDiagHandler)
1357 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1358 else
1359 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001360 return;
1361 }
1362
1363 // Use the CppHashFilename and calculate a line number based on the
1364 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1365 // the diagnostic.
1366 const std::string Filename = Parser->CppHashFilename;
1367
1368 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1369 int CppHashLocLineNo =
1370 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1371 int LineNo = Parser->CppHashLineNumber - 1 +
1372 (DiagLocLineNo - CppHashLocLineNo);
1373
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001374 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1375 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001376 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001377 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001378
Benjamin Kramer04a04262011-10-16 10:48:29 +00001379 if (Parser->SavedDiagHandler)
1380 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1381 else
1382 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001383}
1384
Rafael Espindola65366442011-06-05 02:43:45 +00001385bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1386 const std::vector<StringRef> &Parameters,
1387 const std::vector<std::vector<AsmToken> > &A,
1388 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001389 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001390 unsigned NParameters = Parameters.size();
1391 if (NParameters != 0 && NParameters != A.size())
1392 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001393
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001394 while (!Body.empty()) {
1395 // Scan for the next substitution.
1396 std::size_t End = Body.size(), Pos = 0;
1397 for (; Pos != End; ++Pos) {
1398 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001399 if (!NParameters) {
1400 // This macro has no parameters, look for $0, $1, etc.
1401 if (Body[Pos] != '$' || Pos + 1 == End)
1402 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001403
Rafael Espindola65366442011-06-05 02:43:45 +00001404 char Next = Body[Pos + 1];
1405 if (Next == '$' || Next == 'n' || isdigit(Next))
1406 break;
1407 } else {
1408 // This macro has parameters, look for \foo, \bar, etc.
1409 if (Body[Pos] == '\\' && Pos + 1 != End)
1410 break;
1411 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001412 }
1413
1414 // Add the prefix.
1415 OS << Body.slice(0, Pos);
1416
1417 // Check if we reached the end.
1418 if (Pos == End)
1419 break;
1420
Rafael Espindola65366442011-06-05 02:43:45 +00001421 if (!NParameters) {
1422 switch (Body[Pos+1]) {
1423 // $$ => $
1424 case '$':
1425 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001426 break;
1427
Rafael Espindola65366442011-06-05 02:43:45 +00001428 // $n => number of arguments
1429 case 'n':
1430 OS << A.size();
1431 break;
1432
1433 // $[0-9] => argument
1434 default: {
1435 // Missing arguments are ignored.
1436 unsigned Index = Body[Pos+1] - '0';
1437 if (Index >= A.size())
1438 break;
1439
1440 // Otherwise substitute with the token values, with spaces eliminated.
1441 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1442 ie = A[Index].end(); it != ie; ++it)
1443 OS << it->getString();
1444 break;
1445 }
1446 }
1447 Pos += 2;
1448 } else {
1449 unsigned I = Pos + 1;
1450 while (isalnum(Body[I]) && I + 1 != End)
1451 ++I;
1452
1453 const char *Begin = Body.data() + Pos +1;
1454 StringRef Argument(Begin, I - (Pos +1));
1455 unsigned Index = 0;
1456 for (; Index < NParameters; ++Index)
1457 if (Parameters[Index] == Argument)
1458 break;
1459
1460 // FIXME: We should error at the macro definition.
1461 if (Index == NParameters)
1462 return Error(L, "Parameter not found");
1463
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001464 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1465 ie = A[Index].end(); it != ie; ++it)
1466 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001467
Rafael Espindola65366442011-06-05 02:43:45 +00001468 Pos += 1 + Argument.size();
1469 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001470 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001471 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001472 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001473
1474 // We include the .endmacro in the buffer as our queue to exit the macro
1475 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001476 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001477 return false;
1478}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001479
Rafael Espindola65366442011-06-05 02:43:45 +00001480MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1481 MemoryBuffer *I)
1482 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1483{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001484}
1485
1486bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1487 const Macro *M) {
1488 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1489 // this, although we should protect against infinite loops.
1490 if (ActiveMacros.size() == 20)
1491 return TokError("macros cannot be nested more than 20 levels deep");
1492
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001493 // Parse the macro instantiation arguments.
1494 std::vector<std::vector<AsmToken> > MacroArguments;
1495 MacroArguments.push_back(std::vector<AsmToken>());
1496 unsigned ParenLevel = 0;
1497 for (;;) {
1498 if (Lexer.is(AsmToken::Eof))
1499 return TokError("unexpected token in macro instantiation");
1500 if (Lexer.is(AsmToken::EndOfStatement))
1501 break;
1502
1503 // If we aren't inside parentheses and this is a comma, start a new token
1504 // list.
1505 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1506 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001507 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001508 // Adjust the current parentheses level.
1509 if (Lexer.is(AsmToken::LParen))
1510 ++ParenLevel;
1511 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1512 --ParenLevel;
1513
1514 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001515 MacroArguments.back().push_back(getTok());
1516 }
1517 Lex();
1518 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001519
Rafael Espindola65366442011-06-05 02:43:45 +00001520 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1521 // to hold the macro body with substitutions.
1522 SmallString<256> Buf;
1523 StringRef Body = M->Body;
1524
1525 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1526 return true;
1527
1528 MemoryBuffer *Instantiation =
1529 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1530
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001531 // Create the macro instantiation object and add to the current macro
1532 // instantiation stack.
1533 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001534 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001535 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001536 ActiveMacros.push_back(MI);
1537
1538 // Jump to the macro instantiation and prime the lexer.
1539 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1540 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1541 Lex();
1542
1543 return false;
1544}
1545
1546void AsmParser::HandleMacroExit() {
1547 // Jump to the EndOfStatement we should return to, and consume it.
1548 JumpToLoc(ActiveMacros.back()->ExitLoc);
1549 Lex();
1550
1551 // Pop the instantiation entry.
1552 delete ActiveMacros.back();
1553 ActiveMacros.pop_back();
1554}
1555
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001556static void MarkUsed(const MCExpr *Value) {
1557 switch (Value->getKind()) {
1558 case MCExpr::Binary:
1559 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1560 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1561 break;
1562 case MCExpr::Target:
1563 case MCExpr::Constant:
1564 break;
1565 case MCExpr::SymbolRef: {
1566 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1567 break;
1568 }
1569 case MCExpr::Unary:
1570 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1571 break;
1572 }
1573}
1574
Nico Weber4c4c7322011-01-28 03:04:41 +00001575bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001576 // FIXME: Use better location, we should use proper tokens.
1577 SMLoc EqualLoc = Lexer.getLoc();
1578
Daniel Dunbar821e3332009-08-31 08:09:28 +00001579 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001580 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001581 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001582
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001583 MarkUsed(Value);
1584
Daniel Dunbar3f872332009-07-28 16:08:33 +00001585 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001586 return TokError("unexpected token in assignment");
1587
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001588 // Error on assignment to '.'.
1589 if (Name == ".") {
1590 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1591 "(use '.space' or '.org').)"));
1592 }
1593
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001594 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001595 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001596
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001597 // Validate that the LHS is allowed to be a variable (either it has not been
1598 // used as a symbol, or it is an absolute symbol).
1599 MCSymbol *Sym = getContext().LookupSymbol(Name);
1600 if (Sym) {
1601 // Diagnose assignment to a label.
1602 //
1603 // FIXME: Diagnostics. Note the location of the definition as a label.
1604 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001605 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001606 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001607 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001608 return Error(EqualLoc, "redefinition of '" + Name + "'");
1609 else if (!Sym->isVariable())
1610 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001611 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001612 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1613 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001614
1615 // Don't count these checks as uses.
1616 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001617 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001618 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001619
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001620 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001621
1622 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001623 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001624
1625 return false;
1626}
1627
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001628/// ParseIdentifier:
1629/// ::= identifier
1630/// ::= string
1631bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001632 // The assembler has relaxed rules for accepting identifiers, in particular we
1633 // allow things like '.globl $foo', which would normally be separate
1634 // tokens. At this level, we have already lexed so we cannot (currently)
1635 // handle this as a context dependent token, instead we detect adjacent tokens
1636 // and return the combined identifier.
1637 if (Lexer.is(AsmToken::Dollar)) {
1638 SMLoc DollarLoc = getLexer().getLoc();
1639
1640 // Consume the dollar sign, and check for a following identifier.
1641 Lex();
1642 if (Lexer.isNot(AsmToken::Identifier))
1643 return true;
1644
1645 // We have a '$' followed by an identifier, make sure they are adjacent.
1646 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1647 return true;
1648
1649 // Construct the joined identifier and consume the token.
1650 Res = StringRef(DollarLoc.getPointer(),
1651 getTok().getIdentifier().size() + 1);
1652 Lex();
1653 return false;
1654 }
1655
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001656 if (Lexer.isNot(AsmToken::Identifier) &&
1657 Lexer.isNot(AsmToken::String))
1658 return true;
1659
Sean Callanan18b83232010-01-19 21:44:56 +00001660 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001661
Sean Callanan79ed1a82010-01-19 20:22:31 +00001662 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001663
1664 return false;
1665}
1666
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001667/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001668/// ::= .equ identifier ',' expression
1669/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001670/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001671bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001672 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001673
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001674 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001675 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001676
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001677 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001678 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001679 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001680
Nico Weber4c4c7322011-01-28 03:04:41 +00001681 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001682}
1683
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001684bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001685 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001686
1687 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001688 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001689 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1690 if (Str[i] != '\\') {
1691 Data += Str[i];
1692 continue;
1693 }
1694
1695 // Recognize escaped characters. Note that this escape semantics currently
1696 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1697 ++i;
1698 if (i == e)
1699 return TokError("unexpected backslash at end of string");
1700
1701 // Recognize octal sequences.
1702 if ((unsigned) (Str[i] - '0') <= 7) {
1703 // Consume up to three octal characters.
1704 unsigned Value = Str[i] - '0';
1705
1706 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1707 ++i;
1708 Value = Value * 8 + (Str[i] - '0');
1709
1710 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1711 ++i;
1712 Value = Value * 8 + (Str[i] - '0');
1713 }
1714 }
1715
1716 if (Value > 255)
1717 return TokError("invalid octal escape sequence (out of range)");
1718
1719 Data += (unsigned char) Value;
1720 continue;
1721 }
1722
1723 // Otherwise recognize individual escapes.
1724 switch (Str[i]) {
1725 default:
1726 // Just reject invalid escape sequences for now.
1727 return TokError("invalid escape sequence (unrecognized character)");
1728
1729 case 'b': Data += '\b'; break;
1730 case 'f': Data += '\f'; break;
1731 case 'n': Data += '\n'; break;
1732 case 'r': Data += '\r'; break;
1733 case 't': Data += '\t'; break;
1734 case '"': Data += '"'; break;
1735 case '\\': Data += '\\'; break;
1736 }
1737 }
1738
1739 return false;
1740}
1741
Daniel Dunbara0d14262009-06-24 23:30:00 +00001742/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001743/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1744bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001745 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001746 CheckForValidSection();
1747
Daniel Dunbara0d14262009-06-24 23:30:00 +00001748 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001749 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001750 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001751
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001752 std::string Data;
1753 if (ParseEscapedString(Data))
1754 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001755
1756 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001757 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001758 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1759
Sean Callanan79ed1a82010-01-19 20:22:31 +00001760 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001761
1762 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001763 break;
1764
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001765 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001766 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001767 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001768 }
1769 }
1770
Sean Callanan79ed1a82010-01-19 20:22:31 +00001771 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001772 return false;
1773}
1774
1775/// ParseDirectiveValue
1776/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1777bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001778 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001779 CheckForValidSection();
1780
Daniel Dunbara0d14262009-06-24 23:30:00 +00001781 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001782 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001783 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001784 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001785 return true;
1786
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001787 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001788 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1789 assert(Size <= 8 && "Invalid size");
1790 uint64_t IntValue = MCE->getValue();
1791 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1792 return Error(ExprLoc, "literal value out of range for directive");
1793 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1794 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001795 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001796
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001797 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001798 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001799
Daniel Dunbara0d14262009-06-24 23:30:00 +00001800 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001801 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001802 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001803 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001804 }
1805 }
1806
Sean Callanan79ed1a82010-01-19 20:22:31 +00001807 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001808 return false;
1809}
1810
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001811/// ParseDirectiveRealValue
1812/// ::= (.single | .double) [ expression (, expression)* ]
1813bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1814 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1815 CheckForValidSection();
1816
1817 for (;;) {
1818 // We don't truly support arithmetic on floating point expressions, so we
1819 // have to manually parse unary prefixes.
1820 bool IsNeg = false;
1821 if (getLexer().is(AsmToken::Minus)) {
1822 Lex();
1823 IsNeg = true;
1824 } else if (getLexer().is(AsmToken::Plus))
1825 Lex();
1826
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001827 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001828 getLexer().isNot(AsmToken::Real) &&
1829 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001830 return TokError("unexpected token in directive");
1831
1832 // Convert to an APFloat.
1833 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001834 StringRef IDVal = getTok().getString();
1835 if (getLexer().is(AsmToken::Identifier)) {
1836 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1837 Value = APFloat::getInf(Semantics);
1838 else if (!IDVal.compare_lower("nan"))
1839 Value = APFloat::getNaN(Semantics, false, ~0);
1840 else
1841 return TokError("invalid floating point literal");
1842 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001843 APFloat::opInvalidOp)
1844 return TokError("invalid floating point literal");
1845 if (IsNeg)
1846 Value.changeSign();
1847
1848 // Consume the numeric token.
1849 Lex();
1850
1851 // Emit the value as an integer.
1852 APInt AsInt = Value.bitcastToAPInt();
1853 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1854 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1855
1856 if (getLexer().is(AsmToken::EndOfStatement))
1857 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001858
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001859 if (getLexer().isNot(AsmToken::Comma))
1860 return TokError("unexpected token in directive");
1861 Lex();
1862 }
1863 }
1864
1865 Lex();
1866 return false;
1867}
1868
Daniel Dunbara0d14262009-06-24 23:30:00 +00001869/// ParseDirectiveSpace
1870/// ::= .space expression [ , expression ]
1871bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001872 CheckForValidSection();
1873
Daniel Dunbara0d14262009-06-24 23:30:00 +00001874 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001875 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001876 return true;
1877
1878 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001879 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1880 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001881 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001882 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001883
Daniel Dunbar475839e2009-06-29 20:37:27 +00001884 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001885 return true;
1886
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001887 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001888 return TokError("unexpected token in '.space' directive");
1889 }
1890
Sean Callanan79ed1a82010-01-19 20:22:31 +00001891 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001892
1893 if (NumBytes <= 0)
1894 return TokError("invalid number of bytes in '.space' directive");
1895
1896 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001897 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001898
1899 return false;
1900}
1901
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001902/// ParseDirectiveZero
1903/// ::= .zero expression
1904bool AsmParser::ParseDirectiveZero() {
1905 CheckForValidSection();
1906
1907 int64_t NumBytes;
1908 if (ParseAbsoluteExpression(NumBytes))
1909 return true;
1910
Rafael Espindolae452b172010-10-05 19:42:57 +00001911 int64_t Val = 0;
1912 if (getLexer().is(AsmToken::Comma)) {
1913 Lex();
1914 if (ParseAbsoluteExpression(Val))
1915 return true;
1916 }
1917
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001918 if (getLexer().isNot(AsmToken::EndOfStatement))
1919 return TokError("unexpected token in '.zero' directive");
1920
1921 Lex();
1922
Rafael Espindolae452b172010-10-05 19:42:57 +00001923 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001924
1925 return false;
1926}
1927
Daniel Dunbara0d14262009-06-24 23:30:00 +00001928/// ParseDirectiveFill
1929/// ::= .fill expression , expression , expression
1930bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001931 CheckForValidSection();
1932
Daniel Dunbara0d14262009-06-24 23:30:00 +00001933 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001934 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001935 return true;
1936
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001937 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001938 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001939 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001940
Daniel Dunbara0d14262009-06-24 23:30:00 +00001941 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001942 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001943 return true;
1944
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001945 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001946 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001947 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001948
Daniel Dunbara0d14262009-06-24 23:30:00 +00001949 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001950 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001951 return true;
1952
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001953 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001954 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001955
Sean Callanan79ed1a82010-01-19 20:22:31 +00001956 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001957
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001958 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1959 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001960
1961 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001962 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001963
1964 return false;
1965}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001966
1967/// ParseDirectiveOrg
1968/// ::= .org expression [ , expression ]
1969bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001970 CheckForValidSection();
1971
Daniel Dunbar821e3332009-08-31 08:09:28 +00001972 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00001973 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001974 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001975 return true;
1976
1977 // Parse optional fill expression.
1978 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001979 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1980 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001981 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001982 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001983
Daniel Dunbar475839e2009-06-29 20:37:27 +00001984 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001985 return true;
1986
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001987 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001988 return TokError("unexpected token in '.org' directive");
1989 }
1990
Sean Callanan79ed1a82010-01-19 20:22:31 +00001991 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001992
Jim Grosbachebd4c052012-01-27 00:37:08 +00001993 // Only limited forms of relocatable expressions are accepted here, it
1994 // has to be relative to the current section. The streamer will return
1995 // 'true' if the expression wasn't evaluatable.
1996 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
1997 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00001998
1999 return false;
2000}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002001
2002/// ParseDirectiveAlign
2003/// ::= {.align, ...} expression [ , expression [ , expression ]]
2004bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002005 CheckForValidSection();
2006
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002007 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002008 int64_t Alignment;
2009 if (ParseAbsoluteExpression(Alignment))
2010 return true;
2011
2012 SMLoc MaxBytesLoc;
2013 bool HasFillExpr = false;
2014 int64_t FillExpr = 0;
2015 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002016 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2017 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002018 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002019 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002020
2021 // The fill expression can be omitted while specifying a maximum number of
2022 // alignment bytes, e.g:
2023 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002024 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002025 HasFillExpr = true;
2026 if (ParseAbsoluteExpression(FillExpr))
2027 return true;
2028 }
2029
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002030 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2031 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002032 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002033 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002034
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002035 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002036 if (ParseAbsoluteExpression(MaxBytesToFill))
2037 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002038
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002039 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002040 return TokError("unexpected token in directive");
2041 }
2042 }
2043
Sean Callanan79ed1a82010-01-19 20:22:31 +00002044 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002045
Daniel Dunbar648ac512010-05-17 21:54:30 +00002046 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002047 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002048
2049 // Compute alignment in bytes.
2050 if (IsPow2) {
2051 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002052 if (Alignment >= 32) {
2053 Error(AlignmentLoc, "invalid alignment value");
2054 Alignment = 31;
2055 }
2056
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002057 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002058 }
2059
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002060 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002061 if (MaxBytesLoc.isValid()) {
2062 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002063 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2064 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002065 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002066 }
2067
2068 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002069 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2070 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002071 MaxBytesToFill = 0;
2072 }
2073 }
2074
Daniel Dunbar648ac512010-05-17 21:54:30 +00002075 // Check whether we should use optimal code alignment for this .align
2076 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002077 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002078 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2079 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002080 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002081 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002082 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002083 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2084 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002085 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002086
2087 return false;
2088}
2089
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002090/// ParseDirectiveSymbolAttribute
2091/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002092bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002093 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002094 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002095 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002096 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002097
2098 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002099 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002100
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002101 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002102
Jim Grosbach10ec6502011-09-15 17:56:49 +00002103 // Assembler local symbols don't make any sense here. Complain loudly.
2104 if (Sym->isTemporary())
2105 return Error(Loc, "non-local symbol required in directive");
2106
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002107 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002108
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002109 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002110 break;
2111
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002112 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002113 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002114 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002115 }
2116 }
2117
Sean Callanan79ed1a82010-01-19 20:22:31 +00002118 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002119 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002120}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002121
2122/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002123/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2124bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002125 CheckForValidSection();
2126
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002127 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002128 StringRef Name;
2129 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002130 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002131
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002132 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002133 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002134
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002135 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002136 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002137 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002138
2139 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002140 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002141 if (ParseAbsoluteExpression(Size))
2142 return true;
2143
2144 int64_t Pow2Alignment = 0;
2145 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002146 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002147 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002148 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002149 if (ParseAbsoluteExpression(Pow2Alignment))
2150 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002151
Chris Lattner258281d2010-01-19 06:22:22 +00002152 // If this target takes alignments in bytes (not log) validate and convert.
2153 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2154 if (!isPowerOf2_64(Pow2Alignment))
2155 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2156 Pow2Alignment = Log2_64(Pow2Alignment);
2157 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002158 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002159
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002160 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002161 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002162
Sean Callanan79ed1a82010-01-19 20:22:31 +00002163 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002164
Chris Lattner1fc3d752009-07-09 17:25:12 +00002165 // NOTE: a size of zero for a .comm should create a undefined symbol
2166 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002167 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002168 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2169 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002170
Eric Christopherc260a3e2010-05-14 01:38:54 +00002171 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002172 // may internally end up wanting an alignment in bytes.
2173 // FIXME: Diagnose overflow.
2174 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002175 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2176 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002177
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002178 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002179 return Error(IDLoc, "invalid symbol redefinition");
2180
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002181 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002182 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002183 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002184 getStreamer().EmitZerofill(Ctx.getMachOSection(
2185 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2186 0, SectionKind::getBSS()),
2187 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002188 return false;
2189 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002190
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002191 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002192 return false;
2193}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002194
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002195/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002196/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002197bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002198 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002199 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002200
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002201 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002202 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002203 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002204
Sean Callanan79ed1a82010-01-19 20:22:31 +00002205 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002206
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002207 if (Str.empty())
2208 Error(Loc, ".abort detected. Assembly stopping.");
2209 else
2210 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002211 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002212
2213 return false;
2214}
Kevin Enderby71148242009-07-14 21:35:03 +00002215
Kevin Enderby1f049b22009-07-14 23:21:55 +00002216/// ParseDirectiveInclude
2217/// ::= .include "filename"
2218bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002219 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002220 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002221
Sean Callanan18b83232010-01-19 21:44:56 +00002222 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002223 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002224 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002225
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002226 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002227 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002228
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002229 // Strip the quotes.
2230 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002231
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002232 // Attempt to switch the lexer to the included file before consuming the end
2233 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002234 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002235 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002236 return true;
2237 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002238
2239 return false;
2240}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002241
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002242/// ParseDirectiveIncbin
2243/// ::= .incbin "filename"
2244bool AsmParser::ParseDirectiveIncbin() {
2245 if (getLexer().isNot(AsmToken::String))
2246 return TokError("expected string in '.incbin' directive");
2247
2248 std::string Filename = getTok().getString();
2249 SMLoc IncbinLoc = getLexer().getLoc();
2250 Lex();
2251
2252 if (getLexer().isNot(AsmToken::EndOfStatement))
2253 return TokError("unexpected token in '.incbin' directive");
2254
2255 // Strip the quotes.
2256 Filename = Filename.substr(1, Filename.size()-2);
2257
2258 // Attempt to process the included file.
2259 if (ProcessIncbinFile(Filename)) {
2260 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2261 return true;
2262 }
2263
2264 return false;
2265}
2266
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002267/// ParseDirectiveIf
2268/// ::= .if expression
2269bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002270 TheCondStack.push_back(TheCondState);
2271 TheCondState.TheCond = AsmCond::IfCond;
2272 if(TheCondState.Ignore) {
2273 EatToEndOfStatement();
2274 }
2275 else {
2276 int64_t ExprValue;
2277 if (ParseAbsoluteExpression(ExprValue))
2278 return true;
2279
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002280 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002281 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002282
Sean Callanan79ed1a82010-01-19 20:22:31 +00002283 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002284
2285 TheCondState.CondMet = ExprValue;
2286 TheCondState.Ignore = !TheCondState.CondMet;
2287 }
2288
2289 return false;
2290}
2291
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002292bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2293 StringRef Name;
2294 TheCondStack.push_back(TheCondState);
2295 TheCondState.TheCond = AsmCond::IfCond;
2296
2297 if (TheCondState.Ignore) {
2298 EatToEndOfStatement();
2299 } else {
2300 if (ParseIdentifier(Name))
2301 return TokError("expected identifier after '.ifdef'");
2302
2303 Lex();
2304
2305 MCSymbol *Sym = getContext().LookupSymbol(Name);
2306
2307 if (expect_defined)
2308 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2309 else
2310 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2311 TheCondState.Ignore = !TheCondState.CondMet;
2312 }
2313
2314 return false;
2315}
2316
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002317/// ParseDirectiveElseIf
2318/// ::= .elseif expression
2319bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2320 if (TheCondState.TheCond != AsmCond::IfCond &&
2321 TheCondState.TheCond != AsmCond::ElseIfCond)
2322 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2323 " an .elseif");
2324 TheCondState.TheCond = AsmCond::ElseIfCond;
2325
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002326 bool LastIgnoreState = false;
2327 if (!TheCondStack.empty())
2328 LastIgnoreState = TheCondStack.back().Ignore;
2329 if (LastIgnoreState || TheCondState.CondMet) {
2330 TheCondState.Ignore = true;
2331 EatToEndOfStatement();
2332 }
2333 else {
2334 int64_t ExprValue;
2335 if (ParseAbsoluteExpression(ExprValue))
2336 return true;
2337
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002338 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002339 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002340
Sean Callanan79ed1a82010-01-19 20:22:31 +00002341 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002342 TheCondState.CondMet = ExprValue;
2343 TheCondState.Ignore = !TheCondState.CondMet;
2344 }
2345
2346 return false;
2347}
2348
2349/// ParseDirectiveElse
2350/// ::= .else
2351bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002352 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002353 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002354
Sean Callanan79ed1a82010-01-19 20:22:31 +00002355 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002356
2357 if (TheCondState.TheCond != AsmCond::IfCond &&
2358 TheCondState.TheCond != AsmCond::ElseIfCond)
2359 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2360 ".elseif");
2361 TheCondState.TheCond = AsmCond::ElseCond;
2362 bool LastIgnoreState = false;
2363 if (!TheCondStack.empty())
2364 LastIgnoreState = TheCondStack.back().Ignore;
2365 if (LastIgnoreState || TheCondState.CondMet)
2366 TheCondState.Ignore = true;
2367 else
2368 TheCondState.Ignore = false;
2369
2370 return false;
2371}
2372
2373/// ParseDirectiveEndIf
2374/// ::= .endif
2375bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002376 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002377 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002378
Sean Callanan79ed1a82010-01-19 20:22:31 +00002379 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002380
2381 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2382 TheCondStack.empty())
2383 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2384 ".else");
2385 if (!TheCondStack.empty()) {
2386 TheCondState = TheCondStack.back();
2387 TheCondStack.pop_back();
2388 }
2389
2390 return false;
2391}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002392
2393/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002394/// ::= .file [number] filename
2395/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002396bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002397 // FIXME: I'm not sure what this is.
2398 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002399 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002400 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002401 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002402 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002403
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002404 if (FileNumber < 1)
2405 return TokError("file number less than one");
2406 }
2407
Daniel Dunbareceec052010-07-12 17:45:27 +00002408 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002409 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002410
Nick Lewycky44d798d2011-10-17 23:05:28 +00002411 // Usually the directory and filename together, otherwise just the directory.
2412 StringRef Path = getTok().getString();
2413 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002414 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002415
Nick Lewycky44d798d2011-10-17 23:05:28 +00002416 StringRef Directory;
2417 StringRef Filename;
2418 if (getLexer().is(AsmToken::String)) {
2419 if (FileNumber == -1)
2420 return TokError("explicit path specified, but no file number");
2421 Filename = getTok().getString();
2422 Filename = Filename.substr(1, Filename.size()-2);
2423 Directory = Path;
2424 Lex();
2425 } else {
2426 Filename = Path;
2427 }
2428
Daniel Dunbareceec052010-07-12 17:45:27 +00002429 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002430 return TokError("unexpected token in '.file' directive");
2431
Chris Lattnerd32e8032010-01-25 19:02:58 +00002432 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002433 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002434 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002435 if (getContext().getGenDwarfForAssembly() == true)
2436 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2437 "used to generate dwarf debug info for assembly code");
2438
Nick Lewycky44d798d2011-10-17 23:05:28 +00002439 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002440 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002441 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002442
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002443 return false;
2444}
2445
2446/// ParseDirectiveLine
2447/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002448bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002449 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2450 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002451 return TokError("unexpected token in '.line' directive");
2452
Sean Callanan18b83232010-01-19 21:44:56 +00002453 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002454 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002455 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002456
2457 // FIXME: Do something with the .line.
2458 }
2459
Daniel Dunbareceec052010-07-12 17:45:27 +00002460 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002461 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002462
2463 return false;
2464}
2465
2466
2467/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002468/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002469/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2470/// The first number is a file number, must have been previously assigned with
2471/// a .file directive, the second number is the line number and optionally the
2472/// third number is a column position (zero if not specified). The remaining
2473/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002474bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002475
Daniel Dunbareceec052010-07-12 17:45:27 +00002476 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002477 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002478 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002479 if (FileNumber < 1)
2480 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002481 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002482 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002483 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002484
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002485 int64_t LineNumber = 0;
2486 if (getLexer().is(AsmToken::Integer)) {
2487 LineNumber = getTok().getIntVal();
2488 if (LineNumber < 1)
2489 return TokError("line number less than one in '.loc' directive");
2490 Lex();
2491 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002492
2493 int64_t ColumnPos = 0;
2494 if (getLexer().is(AsmToken::Integer)) {
2495 ColumnPos = getTok().getIntVal();
2496 if (ColumnPos < 0)
2497 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002498 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002499 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002500
Kevin Enderbyc0957932010-09-30 16:52:03 +00002501 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002502 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002503 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002504 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2505 for (;;) {
2506 if (getLexer().is(AsmToken::EndOfStatement))
2507 break;
2508
2509 StringRef Name;
2510 SMLoc Loc = getTok().getLoc();
2511 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002512 return TokError("unexpected token in '.loc' directive");
2513
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002514 if (Name == "basic_block")
2515 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2516 else if (Name == "prologue_end")
2517 Flags |= DWARF2_FLAG_PROLOGUE_END;
2518 else if (Name == "epilogue_begin")
2519 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2520 else if (Name == "is_stmt") {
2521 SMLoc Loc = getTok().getLoc();
2522 const MCExpr *Value;
2523 if (getParser().ParseExpression(Value))
2524 return true;
2525 // The expression must be the constant 0 or 1.
2526 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2527 int Value = MCE->getValue();
2528 if (Value == 0)
2529 Flags &= ~DWARF2_FLAG_IS_STMT;
2530 else if (Value == 1)
2531 Flags |= DWARF2_FLAG_IS_STMT;
2532 else
2533 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002534 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002535 else {
2536 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2537 }
2538 }
2539 else if (Name == "isa") {
2540 SMLoc Loc = getTok().getLoc();
2541 const MCExpr *Value;
2542 if (getParser().ParseExpression(Value))
2543 return true;
2544 // The expression must be a constant greater or equal to 0.
2545 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2546 int Value = MCE->getValue();
2547 if (Value < 0)
2548 return Error(Loc, "isa number less than zero");
2549 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002550 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002551 else {
2552 return Error(Loc, "isa number not a constant value");
2553 }
2554 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002555 else if (Name == "discriminator") {
2556 if (getParser().ParseAbsoluteExpression(Discriminator))
2557 return true;
2558 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002559 else {
2560 return Error(Loc, "unknown sub-directive in '.loc' directive");
2561 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002562
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002563 if (getLexer().is(AsmToken::EndOfStatement))
2564 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002565 }
2566 }
2567
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002568 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002569 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002570
2571 return false;
2572}
2573
Daniel Dunbar138abae2010-10-16 04:56:42 +00002574/// ParseDirectiveStabs
2575/// ::= .stabs string, number, number, number
2576bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2577 SMLoc DirectiveLoc) {
2578 return TokError("unsupported directive '" + Directive + "'");
2579}
2580
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002581/// ParseDirectiveCFISections
2582/// ::= .cfi_sections section [, section]
2583bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2584 SMLoc DirectiveLoc) {
2585 StringRef Name;
2586 bool EH = false;
2587 bool Debug = false;
2588
2589 if (getParser().ParseIdentifier(Name))
2590 return TokError("Expected an identifier");
2591
2592 if (Name == ".eh_frame")
2593 EH = true;
2594 else if (Name == ".debug_frame")
2595 Debug = true;
2596
2597 if (getLexer().is(AsmToken::Comma)) {
2598 Lex();
2599
2600 if (getParser().ParseIdentifier(Name))
2601 return TokError("Expected an identifier");
2602
2603 if (Name == ".eh_frame")
2604 EH = true;
2605 else if (Name == ".debug_frame")
2606 Debug = true;
2607 }
2608
2609 getStreamer().EmitCFISections(EH, Debug);
2610
2611 return false;
2612}
2613
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002614/// ParseDirectiveCFIStartProc
2615/// ::= .cfi_startproc
2616bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2617 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002618 getStreamer().EmitCFIStartProc();
2619 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002620}
2621
2622/// ParseDirectiveCFIEndProc
2623/// ::= .cfi_endproc
2624bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002625 getStreamer().EmitCFIEndProc();
2626 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002627}
2628
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002629/// ParseRegisterOrRegisterNumber - parse register name or number.
2630bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2631 SMLoc DirectiveLoc) {
2632 unsigned RegNo;
2633
Jim Grosbach6f888a82011-06-02 17:14:04 +00002634 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002635 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2636 DirectiveLoc))
2637 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002638 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002639 } else
2640 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002641
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002642 return false;
2643}
2644
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002645/// ParseDirectiveCFIDefCfa
2646/// ::= .cfi_def_cfa register, offset
2647bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2648 SMLoc DirectiveLoc) {
2649 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002650 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002651 return true;
2652
2653 if (getLexer().isNot(AsmToken::Comma))
2654 return TokError("unexpected token in directive");
2655 Lex();
2656
2657 int64_t Offset = 0;
2658 if (getParser().ParseAbsoluteExpression(Offset))
2659 return true;
2660
Rafael Espindola066c2f42011-04-12 23:59:07 +00002661 getStreamer().EmitCFIDefCfa(Register, Offset);
2662 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002663}
2664
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002665/// ParseDirectiveCFIDefCfaOffset
2666/// ::= .cfi_def_cfa_offset offset
2667bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2668 SMLoc DirectiveLoc) {
2669 int64_t Offset = 0;
2670 if (getParser().ParseAbsoluteExpression(Offset))
2671 return true;
2672
Rafael Espindola066c2f42011-04-12 23:59:07 +00002673 getStreamer().EmitCFIDefCfaOffset(Offset);
2674 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002675}
2676
2677/// ParseDirectiveCFIAdjustCfaOffset
2678/// ::= .cfi_adjust_cfa_offset adjustment
2679bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2680 SMLoc DirectiveLoc) {
2681 int64_t Adjustment = 0;
2682 if (getParser().ParseAbsoluteExpression(Adjustment))
2683 return true;
2684
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002685 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2686 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002687}
2688
2689/// ParseDirectiveCFIDefCfaRegister
2690/// ::= .cfi_def_cfa_register register
2691bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2692 SMLoc DirectiveLoc) {
2693 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002694 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002695 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002696
Rafael Espindola066c2f42011-04-12 23:59:07 +00002697 getStreamer().EmitCFIDefCfaRegister(Register);
2698 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002699}
2700
2701/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002702/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002703bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2704 int64_t Register = 0;
2705 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002706
2707 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002708 return true;
2709
2710 if (getLexer().isNot(AsmToken::Comma))
2711 return TokError("unexpected token in directive");
2712 Lex();
2713
2714 if (getParser().ParseAbsoluteExpression(Offset))
2715 return true;
2716
Rafael Espindola066c2f42011-04-12 23:59:07 +00002717 getStreamer().EmitCFIOffset(Register, Offset);
2718 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002719}
2720
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002721/// ParseDirectiveCFIRelOffset
2722/// ::= .cfi_rel_offset register, offset
2723bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2724 SMLoc DirectiveLoc) {
2725 int64_t Register = 0;
2726
2727 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2728 return true;
2729
2730 if (getLexer().isNot(AsmToken::Comma))
2731 return TokError("unexpected token in directive");
2732 Lex();
2733
2734 int64_t Offset = 0;
2735 if (getParser().ParseAbsoluteExpression(Offset))
2736 return true;
2737
Rafael Espindola25f492e2011-04-12 16:12:03 +00002738 getStreamer().EmitCFIRelOffset(Register, Offset);
2739 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002740}
2741
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002742static bool isValidEncoding(int64_t Encoding) {
2743 if (Encoding & ~0xff)
2744 return false;
2745
2746 if (Encoding == dwarf::DW_EH_PE_omit)
2747 return true;
2748
2749 const unsigned Format = Encoding & 0xf;
2750 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2751 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2752 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2753 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2754 return false;
2755
Rafael Espindolacaf11582010-12-29 04:31:26 +00002756 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002757 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002758 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002759 return false;
2760
2761 return true;
2762}
2763
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002764/// ParseDirectiveCFIPersonalityOrLsda
2765/// ::= .cfi_personality encoding, [symbol_name]
2766/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002767bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002768 SMLoc DirectiveLoc) {
2769 int64_t Encoding = 0;
2770 if (getParser().ParseAbsoluteExpression(Encoding))
2771 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002772 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002773 return false;
2774
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002775 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002776 return TokError("unsupported encoding.");
2777
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002778 if (getLexer().isNot(AsmToken::Comma))
2779 return TokError("unexpected token in directive");
2780 Lex();
2781
2782 StringRef Name;
2783 if (getParser().ParseIdentifier(Name))
2784 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002785
2786 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2787
2788 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002789 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002790 else {
2791 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002792 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002793 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002794 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002795}
2796
Rafael Espindolafe024d02010-12-28 18:36:23 +00002797/// ParseDirectiveCFIRememberState
2798/// ::= .cfi_remember_state
2799bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2800 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002801 getStreamer().EmitCFIRememberState();
2802 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002803}
2804
2805/// ParseDirectiveCFIRestoreState
2806/// ::= .cfi_remember_state
2807bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2808 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002809 getStreamer().EmitCFIRestoreState();
2810 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002811}
2812
Rafael Espindolac5754392011-04-12 15:31:05 +00002813/// ParseDirectiveCFISameValue
2814/// ::= .cfi_same_value register
2815bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2816 SMLoc DirectiveLoc) {
2817 int64_t Register = 0;
2818
2819 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2820 return true;
2821
2822 getStreamer().EmitCFISameValue(Register);
2823
2824 return false;
2825}
2826
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00002827/// ParseDirectiveCFIRestore
2828/// ::= .cfi_restore register
2829bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
2830 SMLoc DirectiveLoc) {
2831 int64_t Register = 0;
2832 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2833 return true;
2834
2835 getStreamer().EmitCFIRestore(Register);
2836
2837 return false;
2838}
2839
Rafael Espindola6f0b1812011-12-29 20:24:47 +00002840/// ParseDirectiveCFIEscape
2841/// ::= .cfi_escape expression[,...]
2842bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
2843 SMLoc DirectiveLoc) {
2844 std::string Values;
2845 int64_t CurrValue;
2846 if (getParser().ParseAbsoluteExpression(CurrValue))
2847 return true;
2848
2849 Values.push_back((uint8_t)CurrValue);
2850
2851 while (getLexer().is(AsmToken::Comma)) {
2852 Lex();
2853
2854 if (getParser().ParseAbsoluteExpression(CurrValue))
2855 return true;
2856
2857 Values.push_back((uint8_t)CurrValue);
2858 }
2859
2860 getStreamer().EmitCFIEscape(Values);
2861 return false;
2862}
2863
Rafael Espindola16d7d432012-01-23 21:51:52 +00002864/// ParseDirectiveCFISignalFrame
2865/// ::= .cfi_signal_frame
2866bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
2867 SMLoc DirectiveLoc) {
2868 if (getLexer().isNot(AsmToken::EndOfStatement))
2869 return Error(getLexer().getLoc(),
2870 "unexpected token in '" + Directive + "' directive");
2871
2872 getStreamer().EmitCFISignalFrame();
2873
2874 return false;
2875}
2876
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002877/// ParseDirectiveMacrosOnOff
2878/// ::= .macros_on
2879/// ::= .macros_off
2880bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2881 SMLoc DirectiveLoc) {
2882 if (getLexer().isNot(AsmToken::EndOfStatement))
2883 return Error(getLexer().getLoc(),
2884 "unexpected token in '" + Directive + "' directive");
2885
2886 getParser().MacrosEnabled = Directive == ".macros_on";
2887
2888 return false;
2889}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002890
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002891/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002892/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002893bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2894 SMLoc DirectiveLoc) {
2895 StringRef Name;
2896 if (getParser().ParseIdentifier(Name))
2897 return TokError("expected identifier in directive");
2898
Rafael Espindola65366442011-06-05 02:43:45 +00002899 std::vector<StringRef> Parameters;
2900 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2901 for(;;) {
2902 StringRef Parameter;
2903 if (getParser().ParseIdentifier(Parameter))
2904 return TokError("expected identifier in directive");
2905 Parameters.push_back(Parameter);
2906
2907 if (getLexer().isNot(AsmToken::Comma))
2908 break;
2909 Lex();
2910 }
2911 }
2912
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002913 if (getLexer().isNot(AsmToken::EndOfStatement))
2914 return TokError("unexpected token in '.macro' directive");
2915
2916 // Eat the end of statement.
2917 Lex();
2918
2919 AsmToken EndToken, StartToken = getTok();
2920
2921 // Lex the macro definition.
2922 for (;;) {
2923 // Check whether we have reached the end of the file.
2924 if (getLexer().is(AsmToken::Eof))
2925 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2926
2927 // Otherwise, check whether we have reach the .endmacro.
2928 if (getLexer().is(AsmToken::Identifier) &&
2929 (getTok().getIdentifier() == ".endm" ||
2930 getTok().getIdentifier() == ".endmacro")) {
2931 EndToken = getTok();
2932 Lex();
2933 if (getLexer().isNot(AsmToken::EndOfStatement))
2934 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2935 "' directive");
2936 break;
2937 }
2938
2939 // Otherwise, scan til the end of the statement.
2940 getParser().EatToEndOfStatement();
2941 }
2942
2943 if (getParser().MacroMap.lookup(Name)) {
2944 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2945 }
2946
2947 const char *BodyStart = StartToken.getLoc().getPointer();
2948 const char *BodyEnd = EndToken.getLoc().getPointer();
2949 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002950 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002951 return false;
2952}
2953
2954/// ParseDirectiveEndMacro
2955/// ::= .endm
2956/// ::= .endmacro
2957bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2958 SMLoc DirectiveLoc) {
2959 if (getLexer().isNot(AsmToken::EndOfStatement))
2960 return TokError("unexpected token in '" + Directive + "' directive");
2961
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002962 // If we are inside a macro instantiation, terminate the current
2963 // instantiation.
2964 if (!getParser().ActiveMacros.empty()) {
2965 getParser().HandleMacroExit();
2966 return false;
2967 }
2968
2969 // Otherwise, this .endmacro is a stray entry in the file; well formed
2970 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002971 return TokError("unexpected '" + Directive + "' in file, "
2972 "no current macro definition");
2973}
2974
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002975bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002976 getParser().CheckForValidSection();
2977
2978 const MCExpr *Value;
2979
2980 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002981 return true;
2982
2983 if (getLexer().isNot(AsmToken::EndOfStatement))
2984 return TokError("unexpected token in directive");
2985
2986 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002987 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002988 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002989 getStreamer().EmitULEB128Value(Value);
2990
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002991 return false;
2992}
2993
2994
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002995/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002996MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002997 MCContext &C, MCStreamer &Out,
2998 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002999 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003000}