blob: c5a483c1448ad3c68266d4075b5d00dd26960b34 [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 Espindolae71cc862012-01-28 05:57:00 +00001556static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001557 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001558 case MCExpr::Binary: {
1559 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1560 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001561 break;
1562 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001563 case MCExpr::Target:
1564 case MCExpr::Constant:
1565 return false;
1566 case MCExpr::SymbolRef: {
1567 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001568 if (S.isVariable())
1569 return IsUsedIn(Sym, S.getVariableValue());
1570 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001571 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001572 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001573 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001574 }
1575}
1576
Nico Weber4c4c7322011-01-28 03:04:41 +00001577bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001578 // FIXME: Use better location, we should use proper tokens.
1579 SMLoc EqualLoc = Lexer.getLoc();
1580
Daniel Dunbar821e3332009-08-31 08:09:28 +00001581 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001582 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001583 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001584
Rafael Espindolae71cc862012-01-28 05:57:00 +00001585 // Note: we don't count b as used in "a = b". This is to allow
1586 // a = b
1587 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001588
Daniel Dunbar3f872332009-07-28 16:08:33 +00001589 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001590 return TokError("unexpected token in assignment");
1591
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001592 // Error on assignment to '.'.
1593 if (Name == ".") {
1594 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1595 "(use '.space' or '.org').)"));
1596 }
1597
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001598 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001599 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001600
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001601 // Validate that the LHS is allowed to be a variable (either it has not been
1602 // used as a symbol, or it is an absolute symbol).
1603 MCSymbol *Sym = getContext().LookupSymbol(Name);
1604 if (Sym) {
1605 // Diagnose assignment to a label.
1606 //
1607 // FIXME: Diagnostics. Note the location of the definition as a label.
1608 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001609 if (IsUsedIn(Sym, Value))
1610 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1611 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001612 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001613 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001614 return Error(EqualLoc, "redefinition of '" + Name + "'");
1615 else if (!Sym->isVariable())
1616 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001617 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001618 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1619 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001620
1621 // Don't count these checks as uses.
1622 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001623 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001624 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001625
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001626 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001627
1628 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001629 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001630
1631 return false;
1632}
1633
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001634/// ParseIdentifier:
1635/// ::= identifier
1636/// ::= string
1637bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001638 // The assembler has relaxed rules for accepting identifiers, in particular we
1639 // allow things like '.globl $foo', which would normally be separate
1640 // tokens. At this level, we have already lexed so we cannot (currently)
1641 // handle this as a context dependent token, instead we detect adjacent tokens
1642 // and return the combined identifier.
1643 if (Lexer.is(AsmToken::Dollar)) {
1644 SMLoc DollarLoc = getLexer().getLoc();
1645
1646 // Consume the dollar sign, and check for a following identifier.
1647 Lex();
1648 if (Lexer.isNot(AsmToken::Identifier))
1649 return true;
1650
1651 // We have a '$' followed by an identifier, make sure they are adjacent.
1652 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1653 return true;
1654
1655 // Construct the joined identifier and consume the token.
1656 Res = StringRef(DollarLoc.getPointer(),
1657 getTok().getIdentifier().size() + 1);
1658 Lex();
1659 return false;
1660 }
1661
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001662 if (Lexer.isNot(AsmToken::Identifier) &&
1663 Lexer.isNot(AsmToken::String))
1664 return true;
1665
Sean Callanan18b83232010-01-19 21:44:56 +00001666 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001667
Sean Callanan79ed1a82010-01-19 20:22:31 +00001668 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001669
1670 return false;
1671}
1672
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001673/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001674/// ::= .equ identifier ',' expression
1675/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001676/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001677bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001678 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001679
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001680 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001681 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001682
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001683 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001684 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001685 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001686
Nico Weber4c4c7322011-01-28 03:04:41 +00001687 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001688}
1689
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001690bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001691 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001692
1693 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001694 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001695 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1696 if (Str[i] != '\\') {
1697 Data += Str[i];
1698 continue;
1699 }
1700
1701 // Recognize escaped characters. Note that this escape semantics currently
1702 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1703 ++i;
1704 if (i == e)
1705 return TokError("unexpected backslash at end of string");
1706
1707 // Recognize octal sequences.
1708 if ((unsigned) (Str[i] - '0') <= 7) {
1709 // Consume up to three octal characters.
1710 unsigned Value = Str[i] - '0';
1711
1712 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1713 ++i;
1714 Value = Value * 8 + (Str[i] - '0');
1715
1716 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1717 ++i;
1718 Value = Value * 8 + (Str[i] - '0');
1719 }
1720 }
1721
1722 if (Value > 255)
1723 return TokError("invalid octal escape sequence (out of range)");
1724
1725 Data += (unsigned char) Value;
1726 continue;
1727 }
1728
1729 // Otherwise recognize individual escapes.
1730 switch (Str[i]) {
1731 default:
1732 // Just reject invalid escape sequences for now.
1733 return TokError("invalid escape sequence (unrecognized character)");
1734
1735 case 'b': Data += '\b'; break;
1736 case 'f': Data += '\f'; break;
1737 case 'n': Data += '\n'; break;
1738 case 'r': Data += '\r'; break;
1739 case 't': Data += '\t'; break;
1740 case '"': Data += '"'; break;
1741 case '\\': Data += '\\'; break;
1742 }
1743 }
1744
1745 return false;
1746}
1747
Daniel Dunbara0d14262009-06-24 23:30:00 +00001748/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001749/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1750bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001751 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001752 CheckForValidSection();
1753
Daniel Dunbara0d14262009-06-24 23:30:00 +00001754 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001755 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001756 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001757
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001758 std::string Data;
1759 if (ParseEscapedString(Data))
1760 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001761
1762 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001763 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001764 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1765
Sean Callanan79ed1a82010-01-19 20:22:31 +00001766 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001767
1768 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001769 break;
1770
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001771 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001772 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001773 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001774 }
1775 }
1776
Sean Callanan79ed1a82010-01-19 20:22:31 +00001777 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001778 return false;
1779}
1780
1781/// ParseDirectiveValue
1782/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1783bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001784 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001785 CheckForValidSection();
1786
Daniel Dunbara0d14262009-06-24 23:30:00 +00001787 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001788 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001789 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001790 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001791 return true;
1792
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001793 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001794 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1795 assert(Size <= 8 && "Invalid size");
1796 uint64_t IntValue = MCE->getValue();
1797 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1798 return Error(ExprLoc, "literal value out of range for directive");
1799 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1800 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001801 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001802
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001803 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001804 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001805
Daniel Dunbara0d14262009-06-24 23:30:00 +00001806 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001807 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001808 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001809 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001810 }
1811 }
1812
Sean Callanan79ed1a82010-01-19 20:22:31 +00001813 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001814 return false;
1815}
1816
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001817/// ParseDirectiveRealValue
1818/// ::= (.single | .double) [ expression (, expression)* ]
1819bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1820 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1821 CheckForValidSection();
1822
1823 for (;;) {
1824 // We don't truly support arithmetic on floating point expressions, so we
1825 // have to manually parse unary prefixes.
1826 bool IsNeg = false;
1827 if (getLexer().is(AsmToken::Minus)) {
1828 Lex();
1829 IsNeg = true;
1830 } else if (getLexer().is(AsmToken::Plus))
1831 Lex();
1832
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001833 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001834 getLexer().isNot(AsmToken::Real) &&
1835 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001836 return TokError("unexpected token in directive");
1837
1838 // Convert to an APFloat.
1839 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001840 StringRef IDVal = getTok().getString();
1841 if (getLexer().is(AsmToken::Identifier)) {
1842 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1843 Value = APFloat::getInf(Semantics);
1844 else if (!IDVal.compare_lower("nan"))
1845 Value = APFloat::getNaN(Semantics, false, ~0);
1846 else
1847 return TokError("invalid floating point literal");
1848 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001849 APFloat::opInvalidOp)
1850 return TokError("invalid floating point literal");
1851 if (IsNeg)
1852 Value.changeSign();
1853
1854 // Consume the numeric token.
1855 Lex();
1856
1857 // Emit the value as an integer.
1858 APInt AsInt = Value.bitcastToAPInt();
1859 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1860 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1861
1862 if (getLexer().is(AsmToken::EndOfStatement))
1863 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001864
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001865 if (getLexer().isNot(AsmToken::Comma))
1866 return TokError("unexpected token in directive");
1867 Lex();
1868 }
1869 }
1870
1871 Lex();
1872 return false;
1873}
1874
Daniel Dunbara0d14262009-06-24 23:30:00 +00001875/// ParseDirectiveSpace
1876/// ::= .space expression [ , expression ]
1877bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001878 CheckForValidSection();
1879
Daniel Dunbara0d14262009-06-24 23:30:00 +00001880 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001881 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001882 return true;
1883
1884 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001885 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1886 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001887 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001888 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001889
Daniel Dunbar475839e2009-06-29 20:37:27 +00001890 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001891 return true;
1892
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001893 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001894 return TokError("unexpected token in '.space' directive");
1895 }
1896
Sean Callanan79ed1a82010-01-19 20:22:31 +00001897 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001898
1899 if (NumBytes <= 0)
1900 return TokError("invalid number of bytes in '.space' directive");
1901
1902 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001903 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001904
1905 return false;
1906}
1907
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001908/// ParseDirectiveZero
1909/// ::= .zero expression
1910bool AsmParser::ParseDirectiveZero() {
1911 CheckForValidSection();
1912
1913 int64_t NumBytes;
1914 if (ParseAbsoluteExpression(NumBytes))
1915 return true;
1916
Rafael Espindolae452b172010-10-05 19:42:57 +00001917 int64_t Val = 0;
1918 if (getLexer().is(AsmToken::Comma)) {
1919 Lex();
1920 if (ParseAbsoluteExpression(Val))
1921 return true;
1922 }
1923
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001924 if (getLexer().isNot(AsmToken::EndOfStatement))
1925 return TokError("unexpected token in '.zero' directive");
1926
1927 Lex();
1928
Rafael Espindolae452b172010-10-05 19:42:57 +00001929 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001930
1931 return false;
1932}
1933
Daniel Dunbara0d14262009-06-24 23:30:00 +00001934/// ParseDirectiveFill
1935/// ::= .fill expression , expression , expression
1936bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001937 CheckForValidSection();
1938
Daniel Dunbara0d14262009-06-24 23:30:00 +00001939 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001940 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001941 return true;
1942
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001943 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001944 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001945 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001946
Daniel Dunbara0d14262009-06-24 23:30:00 +00001947 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001948 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001949 return true;
1950
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001951 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001952 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001953 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001954
Daniel Dunbara0d14262009-06-24 23:30:00 +00001955 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001956 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001957 return true;
1958
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001959 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001960 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001961
Sean Callanan79ed1a82010-01-19 20:22:31 +00001962 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001963
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001964 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1965 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001966
1967 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001968 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001969
1970 return false;
1971}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001972
1973/// ParseDirectiveOrg
1974/// ::= .org expression [ , expression ]
1975bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001976 CheckForValidSection();
1977
Daniel Dunbar821e3332009-08-31 08:09:28 +00001978 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00001979 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001980 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001981 return true;
1982
1983 // Parse optional fill expression.
1984 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001985 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1986 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001987 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001988 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001989
Daniel Dunbar475839e2009-06-29 20:37:27 +00001990 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001991 return true;
1992
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001993 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001994 return TokError("unexpected token in '.org' directive");
1995 }
1996
Sean Callanan79ed1a82010-01-19 20:22:31 +00001997 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001998
Jim Grosbachebd4c052012-01-27 00:37:08 +00001999 // Only limited forms of relocatable expressions are accepted here, it
2000 // has to be relative to the current section. The streamer will return
2001 // 'true' if the expression wasn't evaluatable.
2002 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2003 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002004
2005 return false;
2006}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002007
2008/// ParseDirectiveAlign
2009/// ::= {.align, ...} expression [ , expression [ , expression ]]
2010bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002011 CheckForValidSection();
2012
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002013 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002014 int64_t Alignment;
2015 if (ParseAbsoluteExpression(Alignment))
2016 return true;
2017
2018 SMLoc MaxBytesLoc;
2019 bool HasFillExpr = false;
2020 int64_t FillExpr = 0;
2021 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002022 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2023 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002024 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002025 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002026
2027 // The fill expression can be omitted while specifying a maximum number of
2028 // alignment bytes, e.g:
2029 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002030 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002031 HasFillExpr = true;
2032 if (ParseAbsoluteExpression(FillExpr))
2033 return true;
2034 }
2035
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002036 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2037 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002038 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002039 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002040
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002041 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002042 if (ParseAbsoluteExpression(MaxBytesToFill))
2043 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002044
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002045 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002046 return TokError("unexpected token in directive");
2047 }
2048 }
2049
Sean Callanan79ed1a82010-01-19 20:22:31 +00002050 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002051
Daniel Dunbar648ac512010-05-17 21:54:30 +00002052 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002053 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002054
2055 // Compute alignment in bytes.
2056 if (IsPow2) {
2057 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002058 if (Alignment >= 32) {
2059 Error(AlignmentLoc, "invalid alignment value");
2060 Alignment = 31;
2061 }
2062
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002063 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002064 }
2065
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002066 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002067 if (MaxBytesLoc.isValid()) {
2068 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002069 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2070 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002071 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002072 }
2073
2074 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002075 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2076 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002077 MaxBytesToFill = 0;
2078 }
2079 }
2080
Daniel Dunbar648ac512010-05-17 21:54:30 +00002081 // Check whether we should use optimal code alignment for this .align
2082 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002083 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002084 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2085 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002086 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002087 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002088 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002089 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2090 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002091 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002092
2093 return false;
2094}
2095
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002096/// ParseDirectiveSymbolAttribute
2097/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002098bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002099 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002100 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002101 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002102 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002103
2104 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002105 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002106
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002107 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002108
Jim Grosbach10ec6502011-09-15 17:56:49 +00002109 // Assembler local symbols don't make any sense here. Complain loudly.
2110 if (Sym->isTemporary())
2111 return Error(Loc, "non-local symbol required in directive");
2112
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002113 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002114
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002115 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002116 break;
2117
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002118 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002119 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002120 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002121 }
2122 }
2123
Sean Callanan79ed1a82010-01-19 20:22:31 +00002124 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002125 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002126}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002127
2128/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002129/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2130bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002131 CheckForValidSection();
2132
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002133 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002134 StringRef Name;
2135 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002136 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002137
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002138 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002139 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002140
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002141 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002142 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002143 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002144
2145 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002146 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002147 if (ParseAbsoluteExpression(Size))
2148 return true;
2149
2150 int64_t Pow2Alignment = 0;
2151 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002152 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002153 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002154 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002155 if (ParseAbsoluteExpression(Pow2Alignment))
2156 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002157
Chris Lattner258281d2010-01-19 06:22:22 +00002158 // If this target takes alignments in bytes (not log) validate and convert.
2159 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2160 if (!isPowerOf2_64(Pow2Alignment))
2161 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2162 Pow2Alignment = Log2_64(Pow2Alignment);
2163 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002164 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002165
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002166 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002167 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002168
Sean Callanan79ed1a82010-01-19 20:22:31 +00002169 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002170
Chris Lattner1fc3d752009-07-09 17:25:12 +00002171 // NOTE: a size of zero for a .comm should create a undefined symbol
2172 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002173 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002174 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2175 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002176
Eric Christopherc260a3e2010-05-14 01:38:54 +00002177 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002178 // may internally end up wanting an alignment in bytes.
2179 // FIXME: Diagnose overflow.
2180 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002181 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2182 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002183
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002184 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002185 return Error(IDLoc, "invalid symbol redefinition");
2186
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002187 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002188 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002189 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002190 getStreamer().EmitZerofill(Ctx.getMachOSection(
2191 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2192 0, SectionKind::getBSS()),
2193 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002194 return false;
2195 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002196
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002197 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002198 return false;
2199}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002200
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002201/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002202/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002203bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002204 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002205 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002206
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002207 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002208 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002209 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002210
Sean Callanan79ed1a82010-01-19 20:22:31 +00002211 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002212
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002213 if (Str.empty())
2214 Error(Loc, ".abort detected. Assembly stopping.");
2215 else
2216 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002217 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002218
2219 return false;
2220}
Kevin Enderby71148242009-07-14 21:35:03 +00002221
Kevin Enderby1f049b22009-07-14 23:21:55 +00002222/// ParseDirectiveInclude
2223/// ::= .include "filename"
2224bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002225 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002226 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002227
Sean Callanan18b83232010-01-19 21:44:56 +00002228 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002229 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002230 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002231
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002232 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002233 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002234
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002235 // Strip the quotes.
2236 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002237
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002238 // Attempt to switch the lexer to the included file before consuming the end
2239 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002240 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002241 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002242 return true;
2243 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002244
2245 return false;
2246}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002247
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002248/// ParseDirectiveIncbin
2249/// ::= .incbin "filename"
2250bool AsmParser::ParseDirectiveIncbin() {
2251 if (getLexer().isNot(AsmToken::String))
2252 return TokError("expected string in '.incbin' directive");
2253
2254 std::string Filename = getTok().getString();
2255 SMLoc IncbinLoc = getLexer().getLoc();
2256 Lex();
2257
2258 if (getLexer().isNot(AsmToken::EndOfStatement))
2259 return TokError("unexpected token in '.incbin' directive");
2260
2261 // Strip the quotes.
2262 Filename = Filename.substr(1, Filename.size()-2);
2263
2264 // Attempt to process the included file.
2265 if (ProcessIncbinFile(Filename)) {
2266 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2267 return true;
2268 }
2269
2270 return false;
2271}
2272
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002273/// ParseDirectiveIf
2274/// ::= .if expression
2275bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002276 TheCondStack.push_back(TheCondState);
2277 TheCondState.TheCond = AsmCond::IfCond;
2278 if(TheCondState.Ignore) {
2279 EatToEndOfStatement();
2280 }
2281 else {
2282 int64_t ExprValue;
2283 if (ParseAbsoluteExpression(ExprValue))
2284 return true;
2285
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002286 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002287 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002288
Sean Callanan79ed1a82010-01-19 20:22:31 +00002289 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002290
2291 TheCondState.CondMet = ExprValue;
2292 TheCondState.Ignore = !TheCondState.CondMet;
2293 }
2294
2295 return false;
2296}
2297
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002298bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2299 StringRef Name;
2300 TheCondStack.push_back(TheCondState);
2301 TheCondState.TheCond = AsmCond::IfCond;
2302
2303 if (TheCondState.Ignore) {
2304 EatToEndOfStatement();
2305 } else {
2306 if (ParseIdentifier(Name))
2307 return TokError("expected identifier after '.ifdef'");
2308
2309 Lex();
2310
2311 MCSymbol *Sym = getContext().LookupSymbol(Name);
2312
2313 if (expect_defined)
2314 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2315 else
2316 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2317 TheCondState.Ignore = !TheCondState.CondMet;
2318 }
2319
2320 return false;
2321}
2322
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002323/// ParseDirectiveElseIf
2324/// ::= .elseif expression
2325bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2326 if (TheCondState.TheCond != AsmCond::IfCond &&
2327 TheCondState.TheCond != AsmCond::ElseIfCond)
2328 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2329 " an .elseif");
2330 TheCondState.TheCond = AsmCond::ElseIfCond;
2331
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002332 bool LastIgnoreState = false;
2333 if (!TheCondStack.empty())
2334 LastIgnoreState = TheCondStack.back().Ignore;
2335 if (LastIgnoreState || TheCondState.CondMet) {
2336 TheCondState.Ignore = true;
2337 EatToEndOfStatement();
2338 }
2339 else {
2340 int64_t ExprValue;
2341 if (ParseAbsoluteExpression(ExprValue))
2342 return true;
2343
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002344 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002345 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002346
Sean Callanan79ed1a82010-01-19 20:22:31 +00002347 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002348 TheCondState.CondMet = ExprValue;
2349 TheCondState.Ignore = !TheCondState.CondMet;
2350 }
2351
2352 return false;
2353}
2354
2355/// ParseDirectiveElse
2356/// ::= .else
2357bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002358 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002359 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002360
Sean Callanan79ed1a82010-01-19 20:22:31 +00002361 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002362
2363 if (TheCondState.TheCond != AsmCond::IfCond &&
2364 TheCondState.TheCond != AsmCond::ElseIfCond)
2365 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2366 ".elseif");
2367 TheCondState.TheCond = AsmCond::ElseCond;
2368 bool LastIgnoreState = false;
2369 if (!TheCondStack.empty())
2370 LastIgnoreState = TheCondStack.back().Ignore;
2371 if (LastIgnoreState || TheCondState.CondMet)
2372 TheCondState.Ignore = true;
2373 else
2374 TheCondState.Ignore = false;
2375
2376 return false;
2377}
2378
2379/// ParseDirectiveEndIf
2380/// ::= .endif
2381bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002382 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002383 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002384
Sean Callanan79ed1a82010-01-19 20:22:31 +00002385 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002386
2387 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2388 TheCondStack.empty())
2389 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2390 ".else");
2391 if (!TheCondStack.empty()) {
2392 TheCondState = TheCondStack.back();
2393 TheCondStack.pop_back();
2394 }
2395
2396 return false;
2397}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002398
2399/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002400/// ::= .file [number] filename
2401/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002402bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002403 // FIXME: I'm not sure what this is.
2404 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002405 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002406 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002407 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002408 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002409
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002410 if (FileNumber < 1)
2411 return TokError("file number less than one");
2412 }
2413
Daniel Dunbareceec052010-07-12 17:45:27 +00002414 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002415 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002416
Nick Lewycky44d798d2011-10-17 23:05:28 +00002417 // Usually the directory and filename together, otherwise just the directory.
2418 StringRef Path = getTok().getString();
2419 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002420 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002421
Nick Lewycky44d798d2011-10-17 23:05:28 +00002422 StringRef Directory;
2423 StringRef Filename;
2424 if (getLexer().is(AsmToken::String)) {
2425 if (FileNumber == -1)
2426 return TokError("explicit path specified, but no file number");
2427 Filename = getTok().getString();
2428 Filename = Filename.substr(1, Filename.size()-2);
2429 Directory = Path;
2430 Lex();
2431 } else {
2432 Filename = Path;
2433 }
2434
Daniel Dunbareceec052010-07-12 17:45:27 +00002435 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002436 return TokError("unexpected token in '.file' directive");
2437
Chris Lattnerd32e8032010-01-25 19:02:58 +00002438 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002439 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002440 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002441 if (getContext().getGenDwarfForAssembly() == true)
2442 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2443 "used to generate dwarf debug info for assembly code");
2444
Nick Lewycky44d798d2011-10-17 23:05:28 +00002445 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002446 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002447 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002448
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002449 return false;
2450}
2451
2452/// ParseDirectiveLine
2453/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002454bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002455 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2456 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002457 return TokError("unexpected token in '.line' directive");
2458
Sean Callanan18b83232010-01-19 21:44:56 +00002459 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002460 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002461 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002462
2463 // FIXME: Do something with the .line.
2464 }
2465
Daniel Dunbareceec052010-07-12 17:45:27 +00002466 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002467 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002468
2469 return false;
2470}
2471
2472
2473/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002474/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002475/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2476/// The first number is a file number, must have been previously assigned with
2477/// a .file directive, the second number is the line number and optionally the
2478/// third number is a column position (zero if not specified). The remaining
2479/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002480bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002481
Daniel Dunbareceec052010-07-12 17:45:27 +00002482 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002483 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002484 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002485 if (FileNumber < 1)
2486 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002487 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002488 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002489 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002490
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002491 int64_t LineNumber = 0;
2492 if (getLexer().is(AsmToken::Integer)) {
2493 LineNumber = getTok().getIntVal();
2494 if (LineNumber < 1)
2495 return TokError("line number less than one in '.loc' directive");
2496 Lex();
2497 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002498
2499 int64_t ColumnPos = 0;
2500 if (getLexer().is(AsmToken::Integer)) {
2501 ColumnPos = getTok().getIntVal();
2502 if (ColumnPos < 0)
2503 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002504 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002505 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002506
Kevin Enderbyc0957932010-09-30 16:52:03 +00002507 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002508 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002509 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002510 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2511 for (;;) {
2512 if (getLexer().is(AsmToken::EndOfStatement))
2513 break;
2514
2515 StringRef Name;
2516 SMLoc Loc = getTok().getLoc();
2517 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002518 return TokError("unexpected token in '.loc' directive");
2519
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002520 if (Name == "basic_block")
2521 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2522 else if (Name == "prologue_end")
2523 Flags |= DWARF2_FLAG_PROLOGUE_END;
2524 else if (Name == "epilogue_begin")
2525 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2526 else if (Name == "is_stmt") {
2527 SMLoc Loc = getTok().getLoc();
2528 const MCExpr *Value;
2529 if (getParser().ParseExpression(Value))
2530 return true;
2531 // The expression must be the constant 0 or 1.
2532 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2533 int Value = MCE->getValue();
2534 if (Value == 0)
2535 Flags &= ~DWARF2_FLAG_IS_STMT;
2536 else if (Value == 1)
2537 Flags |= DWARF2_FLAG_IS_STMT;
2538 else
2539 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002540 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002541 else {
2542 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2543 }
2544 }
2545 else if (Name == "isa") {
2546 SMLoc Loc = getTok().getLoc();
2547 const MCExpr *Value;
2548 if (getParser().ParseExpression(Value))
2549 return true;
2550 // The expression must be a constant greater or equal to 0.
2551 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2552 int Value = MCE->getValue();
2553 if (Value < 0)
2554 return Error(Loc, "isa number less than zero");
2555 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002556 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002557 else {
2558 return Error(Loc, "isa number not a constant value");
2559 }
2560 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002561 else if (Name == "discriminator") {
2562 if (getParser().ParseAbsoluteExpression(Discriminator))
2563 return true;
2564 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002565 else {
2566 return Error(Loc, "unknown sub-directive in '.loc' directive");
2567 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002568
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002569 if (getLexer().is(AsmToken::EndOfStatement))
2570 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002571 }
2572 }
2573
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002574 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002575 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002576
2577 return false;
2578}
2579
Daniel Dunbar138abae2010-10-16 04:56:42 +00002580/// ParseDirectiveStabs
2581/// ::= .stabs string, number, number, number
2582bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2583 SMLoc DirectiveLoc) {
2584 return TokError("unsupported directive '" + Directive + "'");
2585}
2586
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002587/// ParseDirectiveCFISections
2588/// ::= .cfi_sections section [, section]
2589bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2590 SMLoc DirectiveLoc) {
2591 StringRef Name;
2592 bool EH = false;
2593 bool Debug = false;
2594
2595 if (getParser().ParseIdentifier(Name))
2596 return TokError("Expected an identifier");
2597
2598 if (Name == ".eh_frame")
2599 EH = true;
2600 else if (Name == ".debug_frame")
2601 Debug = true;
2602
2603 if (getLexer().is(AsmToken::Comma)) {
2604 Lex();
2605
2606 if (getParser().ParseIdentifier(Name))
2607 return TokError("Expected an identifier");
2608
2609 if (Name == ".eh_frame")
2610 EH = true;
2611 else if (Name == ".debug_frame")
2612 Debug = true;
2613 }
2614
2615 getStreamer().EmitCFISections(EH, Debug);
2616
2617 return false;
2618}
2619
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002620/// ParseDirectiveCFIStartProc
2621/// ::= .cfi_startproc
2622bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2623 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002624 getStreamer().EmitCFIStartProc();
2625 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002626}
2627
2628/// ParseDirectiveCFIEndProc
2629/// ::= .cfi_endproc
2630bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002631 getStreamer().EmitCFIEndProc();
2632 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002633}
2634
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002635/// ParseRegisterOrRegisterNumber - parse register name or number.
2636bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2637 SMLoc DirectiveLoc) {
2638 unsigned RegNo;
2639
Jim Grosbach6f888a82011-06-02 17:14:04 +00002640 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002641 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2642 DirectiveLoc))
2643 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002644 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002645 } else
2646 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002647
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002648 return false;
2649}
2650
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002651/// ParseDirectiveCFIDefCfa
2652/// ::= .cfi_def_cfa register, offset
2653bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2654 SMLoc DirectiveLoc) {
2655 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002656 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002657 return true;
2658
2659 if (getLexer().isNot(AsmToken::Comma))
2660 return TokError("unexpected token in directive");
2661 Lex();
2662
2663 int64_t Offset = 0;
2664 if (getParser().ParseAbsoluteExpression(Offset))
2665 return true;
2666
Rafael Espindola066c2f42011-04-12 23:59:07 +00002667 getStreamer().EmitCFIDefCfa(Register, Offset);
2668 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002669}
2670
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002671/// ParseDirectiveCFIDefCfaOffset
2672/// ::= .cfi_def_cfa_offset offset
2673bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2674 SMLoc DirectiveLoc) {
2675 int64_t Offset = 0;
2676 if (getParser().ParseAbsoluteExpression(Offset))
2677 return true;
2678
Rafael Espindola066c2f42011-04-12 23:59:07 +00002679 getStreamer().EmitCFIDefCfaOffset(Offset);
2680 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002681}
2682
2683/// ParseDirectiveCFIAdjustCfaOffset
2684/// ::= .cfi_adjust_cfa_offset adjustment
2685bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2686 SMLoc DirectiveLoc) {
2687 int64_t Adjustment = 0;
2688 if (getParser().ParseAbsoluteExpression(Adjustment))
2689 return true;
2690
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002691 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2692 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002693}
2694
2695/// ParseDirectiveCFIDefCfaRegister
2696/// ::= .cfi_def_cfa_register register
2697bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2698 SMLoc DirectiveLoc) {
2699 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002700 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002701 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002702
Rafael Espindola066c2f42011-04-12 23:59:07 +00002703 getStreamer().EmitCFIDefCfaRegister(Register);
2704 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002705}
2706
2707/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002708/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002709bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2710 int64_t Register = 0;
2711 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002712
2713 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002714 return true;
2715
2716 if (getLexer().isNot(AsmToken::Comma))
2717 return TokError("unexpected token in directive");
2718 Lex();
2719
2720 if (getParser().ParseAbsoluteExpression(Offset))
2721 return true;
2722
Rafael Espindola066c2f42011-04-12 23:59:07 +00002723 getStreamer().EmitCFIOffset(Register, Offset);
2724 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002725}
2726
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002727/// ParseDirectiveCFIRelOffset
2728/// ::= .cfi_rel_offset register, offset
2729bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2730 SMLoc DirectiveLoc) {
2731 int64_t Register = 0;
2732
2733 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2734 return true;
2735
2736 if (getLexer().isNot(AsmToken::Comma))
2737 return TokError("unexpected token in directive");
2738 Lex();
2739
2740 int64_t Offset = 0;
2741 if (getParser().ParseAbsoluteExpression(Offset))
2742 return true;
2743
Rafael Espindola25f492e2011-04-12 16:12:03 +00002744 getStreamer().EmitCFIRelOffset(Register, Offset);
2745 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002746}
2747
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002748static bool isValidEncoding(int64_t Encoding) {
2749 if (Encoding & ~0xff)
2750 return false;
2751
2752 if (Encoding == dwarf::DW_EH_PE_omit)
2753 return true;
2754
2755 const unsigned Format = Encoding & 0xf;
2756 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2757 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2758 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2759 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2760 return false;
2761
Rafael Espindolacaf11582010-12-29 04:31:26 +00002762 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002763 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002764 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002765 return false;
2766
2767 return true;
2768}
2769
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002770/// ParseDirectiveCFIPersonalityOrLsda
2771/// ::= .cfi_personality encoding, [symbol_name]
2772/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002773bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002774 SMLoc DirectiveLoc) {
2775 int64_t Encoding = 0;
2776 if (getParser().ParseAbsoluteExpression(Encoding))
2777 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002778 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002779 return false;
2780
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002781 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002782 return TokError("unsupported encoding.");
2783
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002784 if (getLexer().isNot(AsmToken::Comma))
2785 return TokError("unexpected token in directive");
2786 Lex();
2787
2788 StringRef Name;
2789 if (getParser().ParseIdentifier(Name))
2790 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002791
2792 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2793
2794 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002795 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002796 else {
2797 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002798 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002799 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002800 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002801}
2802
Rafael Espindolafe024d02010-12-28 18:36:23 +00002803/// ParseDirectiveCFIRememberState
2804/// ::= .cfi_remember_state
2805bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2806 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002807 getStreamer().EmitCFIRememberState();
2808 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002809}
2810
2811/// ParseDirectiveCFIRestoreState
2812/// ::= .cfi_remember_state
2813bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2814 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002815 getStreamer().EmitCFIRestoreState();
2816 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002817}
2818
Rafael Espindolac5754392011-04-12 15:31:05 +00002819/// ParseDirectiveCFISameValue
2820/// ::= .cfi_same_value register
2821bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2822 SMLoc DirectiveLoc) {
2823 int64_t Register = 0;
2824
2825 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2826 return true;
2827
2828 getStreamer().EmitCFISameValue(Register);
2829
2830 return false;
2831}
2832
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00002833/// ParseDirectiveCFIRestore
2834/// ::= .cfi_restore register
2835bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
2836 SMLoc DirectiveLoc) {
2837 int64_t Register = 0;
2838 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2839 return true;
2840
2841 getStreamer().EmitCFIRestore(Register);
2842
2843 return false;
2844}
2845
Rafael Espindola6f0b1812011-12-29 20:24:47 +00002846/// ParseDirectiveCFIEscape
2847/// ::= .cfi_escape expression[,...]
2848bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
2849 SMLoc DirectiveLoc) {
2850 std::string Values;
2851 int64_t CurrValue;
2852 if (getParser().ParseAbsoluteExpression(CurrValue))
2853 return true;
2854
2855 Values.push_back((uint8_t)CurrValue);
2856
2857 while (getLexer().is(AsmToken::Comma)) {
2858 Lex();
2859
2860 if (getParser().ParseAbsoluteExpression(CurrValue))
2861 return true;
2862
2863 Values.push_back((uint8_t)CurrValue);
2864 }
2865
2866 getStreamer().EmitCFIEscape(Values);
2867 return false;
2868}
2869
Rafael Espindola16d7d432012-01-23 21:51:52 +00002870/// ParseDirectiveCFISignalFrame
2871/// ::= .cfi_signal_frame
2872bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
2873 SMLoc DirectiveLoc) {
2874 if (getLexer().isNot(AsmToken::EndOfStatement))
2875 return Error(getLexer().getLoc(),
2876 "unexpected token in '" + Directive + "' directive");
2877
2878 getStreamer().EmitCFISignalFrame();
2879
2880 return false;
2881}
2882
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002883/// ParseDirectiveMacrosOnOff
2884/// ::= .macros_on
2885/// ::= .macros_off
2886bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2887 SMLoc DirectiveLoc) {
2888 if (getLexer().isNot(AsmToken::EndOfStatement))
2889 return Error(getLexer().getLoc(),
2890 "unexpected token in '" + Directive + "' directive");
2891
2892 getParser().MacrosEnabled = Directive == ".macros_on";
2893
2894 return false;
2895}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002896
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002897/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002898/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002899bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2900 SMLoc DirectiveLoc) {
2901 StringRef Name;
2902 if (getParser().ParseIdentifier(Name))
2903 return TokError("expected identifier in directive");
2904
Rafael Espindola65366442011-06-05 02:43:45 +00002905 std::vector<StringRef> Parameters;
2906 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2907 for(;;) {
2908 StringRef Parameter;
2909 if (getParser().ParseIdentifier(Parameter))
2910 return TokError("expected identifier in directive");
2911 Parameters.push_back(Parameter);
2912
2913 if (getLexer().isNot(AsmToken::Comma))
2914 break;
2915 Lex();
2916 }
2917 }
2918
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002919 if (getLexer().isNot(AsmToken::EndOfStatement))
2920 return TokError("unexpected token in '.macro' directive");
2921
2922 // Eat the end of statement.
2923 Lex();
2924
2925 AsmToken EndToken, StartToken = getTok();
2926
2927 // Lex the macro definition.
2928 for (;;) {
2929 // Check whether we have reached the end of the file.
2930 if (getLexer().is(AsmToken::Eof))
2931 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2932
2933 // Otherwise, check whether we have reach the .endmacro.
2934 if (getLexer().is(AsmToken::Identifier) &&
2935 (getTok().getIdentifier() == ".endm" ||
2936 getTok().getIdentifier() == ".endmacro")) {
2937 EndToken = getTok();
2938 Lex();
2939 if (getLexer().isNot(AsmToken::EndOfStatement))
2940 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2941 "' directive");
2942 break;
2943 }
2944
2945 // Otherwise, scan til the end of the statement.
2946 getParser().EatToEndOfStatement();
2947 }
2948
2949 if (getParser().MacroMap.lookup(Name)) {
2950 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2951 }
2952
2953 const char *BodyStart = StartToken.getLoc().getPointer();
2954 const char *BodyEnd = EndToken.getLoc().getPointer();
2955 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002956 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002957 return false;
2958}
2959
2960/// ParseDirectiveEndMacro
2961/// ::= .endm
2962/// ::= .endmacro
2963bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2964 SMLoc DirectiveLoc) {
2965 if (getLexer().isNot(AsmToken::EndOfStatement))
2966 return TokError("unexpected token in '" + Directive + "' directive");
2967
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002968 // If we are inside a macro instantiation, terminate the current
2969 // instantiation.
2970 if (!getParser().ActiveMacros.empty()) {
2971 getParser().HandleMacroExit();
2972 return false;
2973 }
2974
2975 // Otherwise, this .endmacro is a stray entry in the file; well formed
2976 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002977 return TokError("unexpected '" + Directive + "' in file, "
2978 "no current macro definition");
2979}
2980
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002981bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002982 getParser().CheckForValidSection();
2983
2984 const MCExpr *Value;
2985
2986 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002987 return true;
2988
2989 if (getLexer().isNot(AsmToken::EndOfStatement))
2990 return TokError("unexpected token in directive");
2991
2992 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002993 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002994 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002995 getStreamer().EmitULEB128Value(Value);
2996
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002997 return false;
2998}
2999
3000
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003001/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003002MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003003 MCContext &C, MCStreamer &Out,
3004 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003005 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003006}