blob: e8a8491d27e8de0a505476bed20ccd020a485b60 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000020#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000026#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000027#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000028#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000029#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000030#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000031#include "llvm/Support/CommandLine.h"
Benjamin Kramer518ff562012-01-28 15:28:41 +000032#include "llvm/Support/ErrorHandling.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000033#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000034#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000037#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000038#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000039using namespace llvm;
40
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000041static cl::opt<bool>
42FatalAssemblerWarnings("fatal-assembler-warnings",
43 cl::desc("Consider warnings as error"));
44
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000045namespace {
46
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000047/// \brief Helper class for tracking macro definitions.
Rafael Espindola28c1f6662012-06-03 22:41:23 +000048typedef std::vector<AsmToken> MacroArgument;
49
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000050struct Macro {
51 StringRef Name;
52 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000053 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000054
55public:
Rafael Espindola65366442011-06-05 02:43:45 +000056 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
57 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000058};
59
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000060/// \brief Helper class for storing information about an active macro
61/// instantiation.
62struct MacroInstantiation {
63 /// The macro being instantiated.
64 const Macro *TheMacro;
65
66 /// The macro instantiation with substitutions.
67 MemoryBuffer *Instantiation;
68
69 /// The location of the instantiation.
70 SMLoc InstantiationLoc;
71
72 /// The location where parsing should resume upon instantiation completion.
73 SMLoc ExitLoc;
74
75public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000076 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000077 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000078};
79
Daniel Dunbaraef87e32010-07-18 18:31:38 +000080/// \brief The concrete assembly parser instance.
81class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000082 friend class GenericAsmParser;
83
Daniel Dunbaraef87e32010-07-18 18:31:38 +000084 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
85 void operator=(const AsmParser &); // DO NOT IMPLEMENT
86private:
87 AsmLexer Lexer;
88 MCContext &Ctx;
89 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000090 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000091 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000092 SourceMgr::DiagHandlerTy SavedDiagHandler;
93 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000094 MCAsmParserExtension *GenericParser;
95 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000096
Daniel Dunbaraef87e32010-07-18 18:31:38 +000097 /// This is the current buffer index we're lexing from as managed by the
98 /// SourceMgr object.
99 int CurBuffer;
100
101 AsmCond TheCondState;
102 std::vector<AsmCond> TheCondStack;
103
104 /// DirectiveMap - This is a table handlers for directives. Each handler is
105 /// invoked after the directive identifier is read and is responsible for
106 /// parsing and validating the rest of the directive. The handler is passed
107 /// in the directive name and the location of the directive keyword.
108 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000109
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000110 /// MacroMap - Map of currently defined macros.
111 StringMap<Macro*> MacroMap;
112
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000113 /// ActiveMacros - Stack of active macro instantiations.
114 std::vector<MacroInstantiation*> ActiveMacros;
115
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000116 /// ActiveRept - Stack of active .rept directives.
117 std::vector<SMLoc> ActiveRept;
118
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000119 /// Boolean tracking whether macro substitution is enabled.
120 unsigned MacrosEnabled : 1;
121
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000122 /// Flag tracking whether any errors have been encountered.
123 unsigned HadError : 1;
124
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000125 /// The values from the last parsed cpp hash file line comment if any.
126 StringRef CppHashFilename;
127 int64_t CppHashLineNumber;
128 SMLoc CppHashLoc;
129
Devang Patel0db58bf2012-01-31 18:14:05 +0000130 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
131 unsigned AssemblerDialect;
132
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000133public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000134 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000135 const MCAsmInfo &MAI);
136 ~AsmParser();
137
138 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
139
140 void AddDirectiveHandler(MCAsmParserExtension *Object,
141 StringRef Directive,
142 DirectiveHandler Handler) {
143 DirectiveMap[Directive] = std::make_pair(Object, Handler);
144 }
145
146public:
147 /// @name MCAsmParser Interface
148 /// {
149
150 virtual SourceMgr &getSourceManager() { return SrcMgr; }
151 virtual MCAsmLexer &getLexer() { return Lexer; }
152 virtual MCContext &getContext() { return Ctx; }
153 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000154 virtual unsigned getAssemblerDialect() {
155 if (AssemblerDialect == ~0U)
156 return MAI.getAssemblerDialect();
157 else
158 return AssemblerDialect;
159 }
160 virtual void setAssemblerDialect(unsigned i) {
161 AssemblerDialect = i;
162 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000164 virtual bool Warning(SMLoc L, const Twine &Msg,
165 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
166 virtual bool Error(SMLoc L, const Twine &Msg,
167 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000168
169 const AsmToken &Lex();
170
171 bool ParseExpression(const MCExpr *&Res);
172 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
173 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
174 virtual bool ParseAbsoluteExpression(int64_t &Res);
175
176 /// }
177
178private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000179 void CheckForValidSection();
180
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000181 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000182 void EatToEndOfLine();
183 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000185 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000186 bool expandMacro(SmallString<256> &Buf, StringRef Body,
187 const std::vector<StringRef> &Parameters,
Rafael Espindola28c1f6662012-06-03 22:41:23 +0000188 const std::vector<MacroArgument> &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000189 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000190 void HandleMacroExit();
191
192 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000193 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000194 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
195 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000196 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000197 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000198
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000199 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
200 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000201 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
202 /// This returns true on failure.
203 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000204
205 /// \brief Reset the current lexer position to that given by \arg Loc. The
206 /// current token is not set; clients should ensure Lex() is called
207 /// subsequently.
208 void JumpToLoc(SMLoc Loc);
209
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000210 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000211
212 /// \brief Parse up to the end of statement and a return the contents from the
213 /// current token until the end of the statement; the current token on exit
214 /// will be either the EndOfStatement or EOF.
215 StringRef ParseStringToEndOfStatement();
216
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000217 /// \brief Parse until the end of a statement or a comma is encountered,
218 /// return the contents from the current token up to the end or comma.
219 StringRef ParseStringToComma();
220
Nico Weber4c4c7322011-01-28 03:04:41 +0000221 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000222
223 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
224 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
225 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000226 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000227
228 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
229 /// and set \arg Res to the identifier contents.
230 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000231
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000232 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000233
234 // ".ascii", ".asciiz", ".string"
235 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000236 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000237 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000238 bool ParseDirectiveFill(); // ".fill"
239 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000240 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000241 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000242 bool ParseDirectiveOrg(); // ".org"
243 // ".align{,32}", ".p2align{,w,l}"
244 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
245
246 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
247 /// accepts a single symbol (which should be a label or an external).
248 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000249
250 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
251
252 bool ParseDirectiveAbort(); // ".abort"
253 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000254 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000255
256 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000257 // ".ifb" or ".ifnb", depending on ExpectBlank.
258 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000259 // ".ifc" or ".ifnc", depending on ExpectEqual.
260 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000261 // ".ifdef" or ".ifndef", depending on expect_defined
262 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000263 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
264 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
265 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
266
267 /// ParseEscapedString - Parse the current token as a string which may include
268 /// escaped characters and return the string contents.
269 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000270
271 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
272 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000273
274 bool ParseDirectiveRept(SMLoc DirectiveLoc);
275 bool ParseDirectiveEndRept(SMLoc DirectiveLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000276};
277
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000278/// \brief Generic implementations of directive handling, etc. which is shared
279/// (or the default, at least) for all assembler parser.
280class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000281 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
282 void AddDirectiveHandler(StringRef Directive) {
283 getParser().AddDirectiveHandler(this, Directive,
284 HandleDirective<GenericAsmParser, Handler>);
285 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000286public:
287 GenericAsmParser() {}
288
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000289 AsmParser &getParser() {
290 return (AsmParser&) this->MCAsmParserExtension::getParser();
291 }
292
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000293 virtual void Initialize(MCAsmParser &Parser) {
294 // Call the base implementation.
295 this->MCAsmParserExtension::Initialize(Parser);
296
297 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000298 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
299 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
300 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000301 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000302
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000303 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
305 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
307 ".cfi_startproc");
308 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
309 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000310 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
311 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000312 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
313 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000314 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
315 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000316 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
317 ".cfi_def_cfa_register");
318 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
319 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000320 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
321 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000322 AddDirectiveHandler<
323 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
324 AddDirectiveHandler<
325 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000326 AddDirectiveHandler<
327 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
328 AddDirectiveHandler<
329 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000330 AddDirectiveHandler<
331 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000332 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000333 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
334 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000335 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000336 AddDirectiveHandler<
337 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000338
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000339 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000340 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
341 ".macros_on");
342 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
343 ".macros_off");
344 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
345 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
346 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000347 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000348
349 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
350 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000351 }
352
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000353 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
354
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000355 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
356 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
357 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000358 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000359 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000360 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
361 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000362 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000363 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000364 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000365 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
366 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000367 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000368 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000369 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
370 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000371 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000372 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000373 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000374 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000375
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000376 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000377 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
378 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000379 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000380
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000381 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000382};
383
384}
385
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000386namespace llvm {
387
388extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000389extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000390extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000391
392}
393
Chris Lattneraaec2052010-01-19 19:46:13 +0000394enum { DEFAULT_ADDRSPACE = 0 };
395
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000396AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000397 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000398 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000399 GenericParser(new GenericAsmParser), PlatformParser(0),
Devang Patel0db58bf2012-01-31 18:14:05 +0000400 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
401 AssemblerDialect(~0U) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000402 // Save the old handler.
403 SavedDiagHandler = SrcMgr.getDiagHandler();
404 SavedDiagContext = SrcMgr.getDiagContext();
405 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000406 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000407 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000408
409 // Initialize the generic parser.
410 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000411
412 // Initialize the platform / file format parser.
413 //
414 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
415 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000416 if (_MAI.hasMicrosoftFastStdCallMangling()) {
417 PlatformParser = createCOFFAsmParser();
418 PlatformParser->Initialize(*this);
419 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000420 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000421 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000422 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000423 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000424 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000425 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000426}
427
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000428AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000429 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
430
431 // Destroy any macros.
432 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
433 ie = MacroMap.end(); it != ie; ++it)
434 delete it->getValue();
435
Daniel Dunbare4749702010-07-12 18:12:02 +0000436 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000437 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000438}
439
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000440void AsmParser::PrintMacroInstantiations() {
441 // Print the active macro instantiation stack.
442 for (std::vector<MacroInstantiation*>::const_reverse_iterator
443 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000444 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
445 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000446}
447
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000448bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000449 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000450 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000451 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000452 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000453 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000454}
455
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000456bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000457 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000458 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000459 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000460 return true;
461}
462
Sean Callananfd0b0282010-01-21 00:19:58 +0000463bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000464 std::string IncludedFile;
465 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000466 if (NewBuf == -1)
467 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000468
Sean Callananfd0b0282010-01-21 00:19:58 +0000469 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000470
Sean Callananfd0b0282010-01-21 00:19:58 +0000471 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000472
Sean Callananfd0b0282010-01-21 00:19:58 +0000473 return false;
474}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000475
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000476/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000477/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000478/// returns true on failure.
479bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
480 std::string IncludedFile;
481 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
482 if (NewBuf == -1)
483 return true;
484
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000485 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000486 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
487 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000488 return false;
489}
490
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000491void AsmParser::JumpToLoc(SMLoc Loc) {
492 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
493 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
494}
495
Sean Callananfd0b0282010-01-21 00:19:58 +0000496const AsmToken &AsmParser::Lex() {
497 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000498
Sean Callananfd0b0282010-01-21 00:19:58 +0000499 if (tok->is(AsmToken::Eof)) {
500 // If this is the end of an included file, pop the parent file off the
501 // include stack.
502 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
503 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000504 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000505 tok = &Lexer.Lex();
506 }
507 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000508
Sean Callananfd0b0282010-01-21 00:19:58 +0000509 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000510 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000511
Sean Callananfd0b0282010-01-21 00:19:58 +0000512 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000513}
514
Chris Lattner79180e22010-04-05 23:15:42 +0000515bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000516 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000517 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000518 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000519
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000520 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000521 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000522
523 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000524 AsmCond StartingCondState = TheCondState;
525
Kevin Enderby613b7572011-11-01 22:27:22 +0000526 // If we are generating dwarf for assembly source files save the initial text
527 // section and generate a .file directive.
528 if (getContext().getGenDwarfForAssembly()) {
529 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000530 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
531 getStreamer().EmitLabel(SectionStartSym);
532 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000533 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
534 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
535 }
536
Chris Lattnerb717fb02009-07-02 21:53:43 +0000537 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000538 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000539 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000540
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000541 // We had an error, validate that one was emitted and recover by skipping to
542 // the next line.
543 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000544 EatToEndOfStatement();
545 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000546
547 if (TheCondState.TheCond != StartingCondState.TheCond ||
548 TheCondState.Ignore != StartingCondState.Ignore)
549 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000550
551 // Check to see there are no empty DwarfFile slots.
552 const std::vector<MCDwarfFile *> &MCDwarfFiles =
553 getContext().getMCDwarfFiles();
554 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000555 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000556 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000557 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000558
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000559 // Check to see that all assembler local symbols were actually defined.
560 // Targets that don't do subsections via symbols may not want this, though,
561 // so conservatively exclude them. Only do this if we're finalizing, though,
562 // as otherwise we won't necessarilly have seen everything yet.
563 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
564 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
565 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
566 e = Symbols.end();
567 i != e; ++i) {
568 MCSymbol *Sym = i->getValue();
569 // Variable symbols may not be marked as defined, so check those
570 // explicitly. If we know it's a variable, we have a definition for
571 // the purposes of this check.
572 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
573 // FIXME: We would really like to refer back to where the symbol was
574 // first referenced for a source location. We need to add something
575 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000576 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
577 "assembler local symbol '" + Sym->getName() +
578 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000579 }
580 }
581
582
Chris Lattner79180e22010-04-05 23:15:42 +0000583 // Finalize the output stream if there are no errors and if the client wants
584 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000585 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000586 Out.Finish();
587
Chris Lattnerb717fb02009-07-02 21:53:43 +0000588 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000589}
590
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000591void AsmParser::CheckForValidSection() {
592 if (!getStreamer().getCurrentSection()) {
593 TokError("expected section directive before assembly directive");
594 Out.SwitchSection(Ctx.getMachOSection(
595 "__TEXT", "__text",
596 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
597 0, SectionKind::getText()));
598 }
599}
600
Chris Lattner2cf5f142009-06-22 01:29:09 +0000601/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
602void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000603 while (Lexer.isNot(AsmToken::EndOfStatement) &&
604 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000605 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000606
Chris Lattner2cf5f142009-06-22 01:29:09 +0000607 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000608 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000609 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000610}
611
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000612StringRef AsmParser::ParseStringToEndOfStatement() {
613 const char *Start = getTok().getLoc().getPointer();
614
615 while (Lexer.isNot(AsmToken::EndOfStatement) &&
616 Lexer.isNot(AsmToken::Eof))
617 Lex();
618
619 const char *End = getTok().getLoc().getPointer();
620 return StringRef(Start, End - Start);
621}
Chris Lattnerc4193832009-06-22 05:51:26 +0000622
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000623StringRef AsmParser::ParseStringToComma() {
624 const char *Start = getTok().getLoc().getPointer();
625
626 while (Lexer.isNot(AsmToken::EndOfStatement) &&
627 Lexer.isNot(AsmToken::Comma) &&
628 Lexer.isNot(AsmToken::Eof))
629 Lex();
630
631 const char *End = getTok().getLoc().getPointer();
632 return StringRef(Start, End - Start);
633}
634
Chris Lattner74ec1a32009-06-22 06:32:03 +0000635/// ParseParenExpr - Parse a paren expression and return it.
636/// NOTE: This assumes the leading '(' has already been consumed.
637///
638/// parenexpr ::= expr)
639///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000640bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000641 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000642 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000643 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000644 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000645 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000646 return false;
647}
Chris Lattnerc4193832009-06-22 05:51:26 +0000648
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000649/// ParseBracketExpr - Parse a bracket expression and return it.
650/// NOTE: This assumes the leading '[' has already been consumed.
651///
652/// bracketexpr ::= expr]
653///
654bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
655 if (ParseExpression(Res)) return true;
656 if (Lexer.isNot(AsmToken::RBrac))
657 return TokError("expected ']' in brackets expression");
658 EndLoc = Lexer.getLoc();
659 Lex();
660 return false;
661}
662
Chris Lattner74ec1a32009-06-22 06:32:03 +0000663/// ParsePrimaryExpr - Parse a primary expression and return it.
664/// primaryexpr ::= (parenexpr
665/// primaryexpr ::= symbol
666/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000667/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000668/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000669bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000670 switch (Lexer.getKind()) {
671 default:
672 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000673 // If we have an error assume that we've already handled it.
674 case AsmToken::Error:
675 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000676 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000677 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000678 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000679 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000680 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000681 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000682 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000683 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000684 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000685 EndLoc = Lexer.getLoc();
686
687 StringRef Identifier;
688 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000689 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000690
Daniel Dunbarfffff912009-10-16 01:34:54 +0000691 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000692 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000693 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000694
695 // Lookup the symbol variant if used.
696 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000697 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000698 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000699 if (Variant == MCSymbolRefExpr::VK_Invalid) {
700 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000701 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000702 }
703 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000704
Daniel Dunbarfffff912009-10-16 01:34:54 +0000705 // If this is an absolute variable reference, substitute it now to preserve
706 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000707 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000708 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000709 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000710
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000711 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000712 return false;
713 }
714
715 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000716 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000717 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000718 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000719 case AsmToken::Integer: {
720 SMLoc Loc = getTok().getLoc();
721 int64_t IntVal = getTok().getIntVal();
722 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000723 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000724 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000725 // Look for 'b' or 'f' following an Integer as a directional label
726 if (Lexer.getKind() == AsmToken::Identifier) {
727 StringRef IDVal = getTok().getString();
728 if (IDVal == "f" || IDVal == "b"){
729 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
730 IDVal == "f" ? 1 : 0);
731 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
732 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000733 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000734 return Error(Loc, "invalid reference to undefined symbol");
735 EndLoc = Lexer.getLoc();
736 Lex(); // Eat identifier.
737 }
738 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000739 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000740 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000741 case AsmToken::Real: {
742 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000743 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000744 Res = MCConstantExpr::Create(IntVal, getContext());
745 Lex(); // Eat token.
746 return false;
747 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000748 case AsmToken::Dot: {
749 // This is a '.' reference, which references the current PC. Emit a
750 // temporary label to the streamer and refer to it.
751 MCSymbol *Sym = Ctx.CreateTempSymbol();
752 Out.EmitLabel(Sym);
753 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
754 EndLoc = Lexer.getLoc();
755 Lex(); // Eat identifier.
756 return false;
757 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000758 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000759 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000760 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000761 case AsmToken::LBrac:
762 if (!PlatformParser->HasBracketExpressions())
763 return TokError("brackets expression not supported on this target");
764 Lex(); // Eat the '['.
765 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000766 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000767 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000768 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000769 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000770 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000771 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000772 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000773 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000774 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000775 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000776 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000777 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000778 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000779 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000780 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000781 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000782 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000783 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000784 }
785}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000786
Chris Lattnerb4307b32010-01-15 19:28:38 +0000787bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000788 SMLoc EndLoc;
789 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000790}
791
Daniel Dunbarcceba832010-09-17 02:47:07 +0000792const MCExpr *
793AsmParser::ApplyModifierToExpr(const MCExpr *E,
794 MCSymbolRefExpr::VariantKind Variant) {
795 // Recurse over the given expression, rebuilding it to apply the given variant
796 // if there is exactly one symbol.
797 switch (E->getKind()) {
798 case MCExpr::Target:
799 case MCExpr::Constant:
800 return 0;
801
802 case MCExpr::SymbolRef: {
803 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
804
805 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
806 TokError("invalid variant on expression '" +
807 getTok().getIdentifier() + "' (already modified)");
808 return E;
809 }
810
811 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
812 }
813
814 case MCExpr::Unary: {
815 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
816 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
817 if (!Sub)
818 return 0;
819 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
820 }
821
822 case MCExpr::Binary: {
823 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
824 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
825 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
826
827 if (!LHS && !RHS)
828 return 0;
829
830 if (!LHS) LHS = BE->getLHS();
831 if (!RHS) RHS = BE->getRHS();
832
833 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
834 }
835 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000836
Craig Topper85814382012-02-07 05:05:23 +0000837 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000838}
839
Chris Lattner74ec1a32009-06-22 06:32:03 +0000840/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000841///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000842/// expr ::= expr &&,|| expr -> lowest.
843/// expr ::= expr |,^,&,! expr
844/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
845/// expr ::= expr <<,>> expr
846/// expr ::= expr +,- expr
847/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000848/// expr ::= primaryexpr
849///
Chris Lattner54482b42010-01-15 19:39:23 +0000850bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000851 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000852 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000853 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
854 return true;
855
Daniel Dunbarcceba832010-09-17 02:47:07 +0000856 // As a special case, we support 'a op b @ modifier' by rewriting the
857 // expression to include the modifier. This is inefficient, but in general we
858 // expect users to use 'a@modifier op b'.
859 if (Lexer.getKind() == AsmToken::At) {
860 Lex();
861
862 if (Lexer.isNot(AsmToken::Identifier))
863 return TokError("unexpected symbol modifier following '@'");
864
865 MCSymbolRefExpr::VariantKind Variant =
866 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
867 if (Variant == MCSymbolRefExpr::VK_Invalid)
868 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
869
870 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
871 if (!ModifiedRes) {
872 return TokError("invalid modifier '" + getTok().getIdentifier() +
873 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000874 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000875
Daniel Dunbarcceba832010-09-17 02:47:07 +0000876 Res = ModifiedRes;
877 Lex();
878 }
879
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000880 // Try to constant fold it up front, if possible.
881 int64_t Value;
882 if (Res->EvaluateAsAbsolute(Value))
883 Res = MCConstantExpr::Create(Value, getContext());
884
885 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000886}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000887
Chris Lattnerb4307b32010-01-15 19:28:38 +0000888bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000889 Res = 0;
890 return ParseParenExpr(Res, EndLoc) ||
891 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000892}
893
Daniel Dunbar475839e2009-06-29 20:37:27 +0000894bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000895 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000896
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000897 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000898 if (ParseExpression(Expr))
899 return true;
900
Daniel Dunbare00b0112009-10-16 01:57:52 +0000901 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000902 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000903
904 return false;
905}
906
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000907static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000908 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000909 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000910 default:
911 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000912
Jim Grosbachfbe16812011-08-20 16:24:13 +0000913 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000914 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000915 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000916 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000917 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000918 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000919 return 1;
920
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000921
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000922 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000923 //
924 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000925 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000926 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000927 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000928 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000929 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000930 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000931 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000932 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000933 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000934
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000935 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000936 case AsmToken::EqualEqual:
937 Kind = MCBinaryExpr::EQ;
938 return 3;
939 case AsmToken::ExclaimEqual:
940 case AsmToken::LessGreater:
941 Kind = MCBinaryExpr::NE;
942 return 3;
943 case AsmToken::Less:
944 Kind = MCBinaryExpr::LT;
945 return 3;
946 case AsmToken::LessEqual:
947 Kind = MCBinaryExpr::LTE;
948 return 3;
949 case AsmToken::Greater:
950 Kind = MCBinaryExpr::GT;
951 return 3;
952 case AsmToken::GreaterEqual:
953 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000954 return 3;
955
Jim Grosbachfbe16812011-08-20 16:24:13 +0000956 // Intermediate Precedence: <<, >>
957 case AsmToken::LessLess:
958 Kind = MCBinaryExpr::Shl;
959 return 4;
960 case AsmToken::GreaterGreater:
961 Kind = MCBinaryExpr::Shr;
962 return 4;
963
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000964 // High Intermediate Precedence: +, -
965 case AsmToken::Plus:
966 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000967 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000968 case AsmToken::Minus:
969 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000970 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000971
Jim Grosbachfbe16812011-08-20 16:24:13 +0000972 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000973 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000974 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000975 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000976 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000977 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000978 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000979 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000980 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000981 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000982 }
983}
984
985
986/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
987/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000988bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
989 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000990 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000991 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000992 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000993
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000994 // If the next token is lower precedence than we are allowed to eat, return
995 // successfully with what we ate already.
996 if (TokPrec < Precedence)
997 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000998
Sean Callanan79ed1a82010-01-19 20:22:31 +0000999 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001000
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001001 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001002 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001003 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001004
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001005 // If BinOp binds less tightly with RHS than the operator after RHS, let
1006 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001007 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001008 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001009 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001010 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001011 }
1012
Daniel Dunbar475839e2009-06-29 20:37:27 +00001013 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001014 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001015 }
1016}
1017
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001018
1019
1020
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001021/// ParseStatement:
1022/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001023/// ::= Label* Directive ...Operands... EndOfStatement
1024/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001025bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001026 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001027 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001028 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001029 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001030 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001031
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001032 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001033 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001034 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001035 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001036 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001037 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001038 if (Lexer.is(AsmToken::Hash))
1039 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001040
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001041 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001042 if (Lexer.is(AsmToken::Integer)) {
1043 LocalLabelVal = getTok().getIntVal();
1044 if (LocalLabelVal < 0) {
1045 if (!TheCondState.Ignore)
1046 return TokError("unexpected token at start of statement");
1047 IDVal = "";
1048 }
1049 else {
1050 IDVal = getTok().getString();
1051 Lex(); // Consume the integer token to be used as an identifier token.
1052 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001053 if (!TheCondState.Ignore)
1054 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001055 }
1056 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001057
1058 } else if (Lexer.is(AsmToken::Dot)) {
1059 // Treat '.' as a valid identifier in this context.
1060 Lex();
1061 IDVal = ".";
1062
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001063 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001064 if (!TheCondState.Ignore)
1065 return TokError("unexpected token at start of statement");
1066 IDVal = "";
1067 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001068
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001069
Chris Lattner7834fac2010-04-17 18:14:27 +00001070 // Handle conditional assembly here before checking for skipping. We
1071 // have to do this so that .endif isn't skipped in a ".if 0" block for
1072 // example.
1073 if (IDVal == ".if")
1074 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001075 if (IDVal == ".ifb")
1076 return ParseDirectiveIfb(IDLoc, true);
1077 if (IDVal == ".ifnb")
1078 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00001079 if (IDVal == ".ifc")
1080 return ParseDirectiveIfc(IDLoc, true);
1081 if (IDVal == ".ifnc")
1082 return ParseDirectiveIfc(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001083 if (IDVal == ".ifdef")
1084 return ParseDirectiveIfdef(IDLoc, true);
1085 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1086 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001087 if (IDVal == ".elseif")
1088 return ParseDirectiveElseIf(IDLoc);
1089 if (IDVal == ".else")
1090 return ParseDirectiveElse(IDLoc);
1091 if (IDVal == ".endif")
1092 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001093
Chris Lattner7834fac2010-04-17 18:14:27 +00001094 // If we are in a ".if 0" block, ignore this statement.
1095 if (TheCondState.Ignore) {
1096 EatToEndOfStatement();
1097 return false;
1098 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001099
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001100 // FIXME: Recurse on local labels?
1101
1102 // See what kind of statement we have.
1103 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001104 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001105 CheckForValidSection();
1106
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001107 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001108 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001109
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001110 // Diagnose attempt to use '.' as a label.
1111 if (IDVal == ".")
1112 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1113
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001114 // Diagnose attempt to use a variable as a label.
1115 //
1116 // FIXME: Diagnostics. Note the location of the definition as a label.
1117 // FIXME: This doesn't diagnose assignment to a symbol which has been
1118 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001119 MCSymbol *Sym;
1120 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001121 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001122 else
1123 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001124 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001125 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001126
Daniel Dunbar959fd882009-08-26 22:13:22 +00001127 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001128 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001129
Kevin Enderby94c2e852011-12-09 18:09:40 +00001130 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001131 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001132 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001133 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1134 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001135
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001136 // Consume any end of statement token, if present, to avoid spurious
1137 // AddBlankLine calls().
1138 if (Lexer.is(AsmToken::EndOfStatement)) {
1139 Lex();
1140 if (Lexer.is(AsmToken::Eof))
1141 return false;
1142 }
1143
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001144 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001145 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001146
Daniel Dunbar3f872332009-07-28 16:08:33 +00001147 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001148 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001149 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001150
Nico Weber4c4c7322011-01-28 03:04:41 +00001151 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001152
1153 default: // Normal instruction or directive.
1154 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001155 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001156
1157 // If macros are enabled, check to see if this is a macro instantiation.
1158 if (MacrosEnabled)
1159 if (const Macro *M = MacroMap.lookup(IDVal))
1160 return HandleMacroEntry(IDVal, IDLoc, M);
1161
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001162 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001163 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001164 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001165 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001166 return ParseDirectiveSet(IDVal, true);
1167 if (IDVal == ".equiv")
1168 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001169
Daniel Dunbara0d14262009-06-24 23:30:00 +00001170 // Data directives
1171
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001172 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001173 return ParseDirectiveAscii(IDVal, false);
1174 if (IDVal == ".asciz" || IDVal == ".string")
1175 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001176
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001177 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001178 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001179 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001180 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001181 if (IDVal == ".value")
1182 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001183 if (IDVal == ".2byte")
1184 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001185 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001186 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001187 if (IDVal == ".int")
1188 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001189 if (IDVal == ".4byte")
1190 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001191 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001192 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001193 if (IDVal == ".8byte")
1194 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001195 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001196 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1197 if (IDVal == ".double")
1198 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001199
Eli Friedman5d68ec22010-07-19 04:17:25 +00001200 if (IDVal == ".align") {
1201 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1202 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1203 }
1204 if (IDVal == ".align32") {
1205 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1206 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1207 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001208 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001209 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001210 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001211 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001212 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001213 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001214 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001215 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001216 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001217 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001218 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001219 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1220
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001221 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001222 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001223
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001224 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001225 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001226 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001227 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001228 if (IDVal == ".zero")
1229 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001230
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001231 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001232
Benjamin Kramere14a3c52012-05-12 11:18:59 +00001233 if (IDVal == ".extern") {
1234 EatToEndOfStatement(); // .extern is the default, ignore it.
1235 return false;
1236 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001237 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001238 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001239 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001240 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001241 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001242 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001243 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001244 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001245 if (IDVal == ".symbol_resolver")
1246 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001247 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001248 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001249 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001250 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001251 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001252 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001253 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001254 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001255 if (IDVal == ".weak_def_can_be_hidden")
1256 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001257
Hans Wennborg5cc64912011-06-18 13:51:54 +00001258 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001259 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001260 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001261 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001262
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001263 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001264 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001265 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001266 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001267 if (IDVal == ".incbin")
1268 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001269
Benjamin Kramer5cdf0ad2012-05-12 11:19:04 +00001270 if (IDVal == ".code16" || IDVal == ".code16gcc")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001271 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001272
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001273 if (IDVal == ".rept")
1274 return ParseDirectiveRept(IDLoc);
1275 if (IDVal == ".endr")
1276 return ParseDirectiveEndRept(IDLoc);
1277
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001278 // Look up the handler in the handler table.
1279 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1280 DirectiveMap.lookup(IDVal);
1281 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001282 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001283
Kevin Enderby9c656452009-09-10 20:51:44 +00001284 // Target hook for parsing target specific directives.
1285 if (!getTargetParser().ParseDirective(ID))
1286 return false;
1287
Jim Grosbach686c0182012-05-01 18:38:27 +00001288 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001289 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001290
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001291 CheckForValidSection();
1292
Chris Lattnera7f13542010-05-19 23:34:33 +00001293 // Canonicalize the opcode to lower case.
1294 SmallString<128> Opcode;
1295 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1296 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001297
Chris Lattner98986712010-01-14 22:21:20 +00001298 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001299 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001300 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001301
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001302 // Dump the parsed representation, if requested.
1303 if (getShowParsedOperands()) {
1304 SmallString<256> Str;
1305 raw_svector_ostream OS(Str);
1306 OS << "parsed instruction: [";
1307 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1308 if (i != 0)
1309 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001310 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001311 }
1312 OS << "]";
1313
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001314 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001315 }
1316
Kevin Enderby613b7572011-11-01 22:27:22 +00001317 // If we are generating dwarf for assembly source files and the current
1318 // section is the initial text section then generate a .loc directive for
1319 // the instruction.
1320 if (!HadError && getContext().getGenDwarfForAssembly() &&
1321 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1322 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1323 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1324 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001325 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001326 StringRef());
1327 }
1328
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001329 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001330 if (!HadError)
1331 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1332 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001333
Chris Lattner98986712010-01-14 22:21:20 +00001334 // Free any parsed operands.
1335 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1336 delete ParsedOperands[i];
1337
Chris Lattnercbf8a982010-09-11 16:18:25 +00001338 // Don't skip the rest of the line, the instruction parser is responsible for
1339 // that.
1340 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001341}
Chris Lattner9a023f72009-06-24 04:43:34 +00001342
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001343/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1344/// since they may not be able to be tokenized to get to the end of line token.
1345void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001346 if (!Lexer.is(AsmToken::EndOfStatement))
1347 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001348 // Eat EOL.
1349 Lex();
1350}
1351
1352/// ParseCppHashLineFilenameComment as this:
1353/// ::= # number "filename"
1354/// or just as a full line comment if it doesn't have a number and a string.
1355bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1356 Lex(); // Eat the hash token.
1357
1358 if (getLexer().isNot(AsmToken::Integer)) {
1359 // Consume the line since in cases it is not a well-formed line directive,
1360 // as if were simply a full line comment.
1361 EatToEndOfLine();
1362 return false;
1363 }
1364
1365 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001366 Lex();
1367
1368 if (getLexer().isNot(AsmToken::String)) {
1369 EatToEndOfLine();
1370 return false;
1371 }
1372
1373 StringRef Filename = getTok().getString();
1374 // Get rid of the enclosing quotes.
1375 Filename = Filename.substr(1, Filename.size()-2);
1376
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001377 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1378 CppHashLoc = L;
1379 CppHashFilename = Filename;
1380 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001381
1382 // Ignore any trailing characters, they're just comment.
1383 EatToEndOfLine();
1384 return false;
1385}
1386
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001387/// DiagHandler - will use the the last parsed cpp hash line filename comment
1388/// for the Filename and LineNo if any in the diagnostic.
1389void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1390 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1391 raw_ostream &OS = errs();
1392
1393 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1394 const SMLoc &DiagLoc = Diag.getLoc();
1395 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1396 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1397
1398 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1399 // before printing the message.
1400 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001401 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001402 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1403 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1404 }
1405
1406 // If we have not parsed a cpp hash line filename comment or the source
1407 // manager changed or buffer changed (like in a nested include) then just
1408 // print the normal diagnostic using its Filename and LineNo.
1409 if (!Parser->CppHashLineNumber ||
1410 &DiagSrcMgr != &Parser->SrcMgr ||
1411 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001412 if (Parser->SavedDiagHandler)
1413 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1414 else
1415 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001416 return;
1417 }
1418
1419 // Use the CppHashFilename and calculate a line number based on the
1420 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1421 // the diagnostic.
1422 const std::string Filename = Parser->CppHashFilename;
1423
1424 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1425 int CppHashLocLineNo =
1426 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1427 int LineNo = Parser->CppHashLineNumber - 1 +
1428 (DiagLocLineNo - CppHashLocLineNo);
1429
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001430 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1431 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001432 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001433 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001434
Benjamin Kramer04a04262011-10-16 10:48:29 +00001435 if (Parser->SavedDiagHandler)
1436 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1437 else
1438 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001439}
1440
Rafael Espindola65366442011-06-05 02:43:45 +00001441bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1442 const std::vector<StringRef> &Parameters,
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001443 const std::vector<MacroArgument> &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001444 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001445 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001446 unsigned NParameters = Parameters.size();
1447 if (NParameters != 0 && NParameters != A.size())
1448 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001449
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001450 while (!Body.empty()) {
1451 // Scan for the next substitution.
1452 std::size_t End = Body.size(), Pos = 0;
1453 for (; Pos != End; ++Pos) {
1454 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001455 if (!NParameters) {
1456 // This macro has no parameters, look for $0, $1, etc.
1457 if (Body[Pos] != '$' || Pos + 1 == End)
1458 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001459
Rafael Espindola65366442011-06-05 02:43:45 +00001460 char Next = Body[Pos + 1];
1461 if (Next == '$' || Next == 'n' || isdigit(Next))
1462 break;
1463 } else {
1464 // This macro has parameters, look for \foo, \bar, etc.
1465 if (Body[Pos] == '\\' && Pos + 1 != End)
1466 break;
1467 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001468 }
1469
1470 // Add the prefix.
1471 OS << Body.slice(0, Pos);
1472
1473 // Check if we reached the end.
1474 if (Pos == End)
1475 break;
1476
Rafael Espindola65366442011-06-05 02:43:45 +00001477 if (!NParameters) {
1478 switch (Body[Pos+1]) {
1479 // $$ => $
1480 case '$':
1481 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001482 break;
1483
Rafael Espindola65366442011-06-05 02:43:45 +00001484 // $n => number of arguments
1485 case 'n':
1486 OS << A.size();
1487 break;
1488
1489 // $[0-9] => argument
1490 default: {
1491 // Missing arguments are ignored.
1492 unsigned Index = Body[Pos+1] - '0';
1493 if (Index >= A.size())
1494 break;
1495
1496 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001497 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001498 ie = A[Index].end(); it != ie; ++it)
1499 OS << it->getString();
1500 break;
1501 }
1502 }
1503 Pos += 2;
1504 } else {
1505 unsigned I = Pos + 1;
1506 while (isalnum(Body[I]) && I + 1 != End)
1507 ++I;
1508
1509 const char *Begin = Body.data() + Pos +1;
1510 StringRef Argument(Begin, I - (Pos +1));
1511 unsigned Index = 0;
1512 for (; Index < NParameters; ++Index)
1513 if (Parameters[Index] == Argument)
1514 break;
1515
1516 // FIXME: We should error at the macro definition.
1517 if (Index == NParameters)
1518 return Error(L, "Parameter not found");
1519
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001520 for (MacroArgument::const_iterator it = A[Index].begin(),
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001521 ie = A[Index].end(); it != ie; ++it)
1522 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001523
Rafael Espindola65366442011-06-05 02:43:45 +00001524 Pos += 1 + Argument.size();
1525 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001526 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001527 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001528 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001529
1530 // We include the .endmacro in the buffer as our queue to exit the macro
1531 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001532 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001533 return false;
1534}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001535
Rafael Espindola65366442011-06-05 02:43:45 +00001536MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1537 MemoryBuffer *I)
1538 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1539{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001540}
1541
1542bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1543 const Macro *M) {
1544 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1545 // this, although we should protect against infinite loops.
1546 if (ActiveMacros.size() == 20)
1547 return TokError("macros cannot be nested more than 20 levels deep");
1548
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001549 // Parse the macro instantiation arguments.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001550 std::vector<MacroArgument> MacroArguments;
1551 MacroArguments.push_back(MacroArgument());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001552 unsigned ParenLevel = 0;
1553 for (;;) {
1554 if (Lexer.is(AsmToken::Eof))
1555 return TokError("unexpected token in macro instantiation");
1556 if (Lexer.is(AsmToken::EndOfStatement))
1557 break;
1558
1559 // If we aren't inside parentheses and this is a comma, start a new token
1560 // list.
1561 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001562 MacroArguments.push_back(MacroArgument());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001563 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001564 // Adjust the current parentheses level.
1565 if (Lexer.is(AsmToken::LParen))
1566 ++ParenLevel;
1567 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1568 --ParenLevel;
1569
1570 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001571 MacroArguments.back().push_back(getTok());
1572 }
1573 Lex();
1574 }
Jim Grosbach68f89a62012-04-16 21:18:49 +00001575 // If the last argument didn't end up with any tokens, it's not a real
1576 // argument and we should remove it from the list. This happens with either
1577 // a tailing comma or an empty argument list.
1578 if (MacroArguments.back().empty())
1579 MacroArguments.pop_back();
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001580
Rafael Espindola65366442011-06-05 02:43:45 +00001581 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1582 // to hold the macro body with substitutions.
1583 SmallString<256> Buf;
1584 StringRef Body = M->Body;
1585
1586 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1587 return true;
1588
1589 MemoryBuffer *Instantiation =
1590 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1591
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001592 // Create the macro instantiation object and add to the current macro
1593 // instantiation stack.
1594 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001595 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001596 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001597 ActiveMacros.push_back(MI);
1598
1599 // Jump to the macro instantiation and prime the lexer.
1600 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1601 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1602 Lex();
1603
1604 return false;
1605}
1606
1607void AsmParser::HandleMacroExit() {
1608 // Jump to the EndOfStatement we should return to, and consume it.
1609 JumpToLoc(ActiveMacros.back()->ExitLoc);
1610 Lex();
1611
1612 // Pop the instantiation entry.
1613 delete ActiveMacros.back();
1614 ActiveMacros.pop_back();
1615}
1616
Rafael Espindolae71cc862012-01-28 05:57:00 +00001617static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001618 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001619 case MCExpr::Binary: {
1620 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1621 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001622 break;
1623 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001624 case MCExpr::Target:
1625 case MCExpr::Constant:
1626 return false;
1627 case MCExpr::SymbolRef: {
1628 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001629 if (S.isVariable())
1630 return IsUsedIn(Sym, S.getVariableValue());
1631 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001632 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001633 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001634 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001635 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001636
1637 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001638}
1639
Nico Weber4c4c7322011-01-28 03:04:41 +00001640bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001641 // FIXME: Use better location, we should use proper tokens.
1642 SMLoc EqualLoc = Lexer.getLoc();
1643
Daniel Dunbar821e3332009-08-31 08:09:28 +00001644 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001645 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001646 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001647
Rafael Espindolae71cc862012-01-28 05:57:00 +00001648 // Note: we don't count b as used in "a = b". This is to allow
1649 // a = b
1650 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001651
Daniel Dunbar3f872332009-07-28 16:08:33 +00001652 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001653 return TokError("unexpected token in assignment");
1654
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001655 // Error on assignment to '.'.
1656 if (Name == ".") {
1657 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1658 "(use '.space' or '.org').)"));
1659 }
1660
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001661 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001662 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001663
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001664 // Validate that the LHS is allowed to be a variable (either it has not been
1665 // used as a symbol, or it is an absolute symbol).
1666 MCSymbol *Sym = getContext().LookupSymbol(Name);
1667 if (Sym) {
1668 // Diagnose assignment to a label.
1669 //
1670 // FIXME: Diagnostics. Note the location of the definition as a label.
1671 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001672 if (IsUsedIn(Sym, Value))
1673 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1674 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001675 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001676 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1677 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001678 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001679 return Error(EqualLoc, "redefinition of '" + Name + "'");
1680 else if (!Sym->isVariable())
1681 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001682 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001683 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1684 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001685
1686 // Don't count these checks as uses.
1687 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001688 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001689 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001690
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001691 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001692
1693 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001694 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001695
1696 return false;
1697}
1698
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001699/// ParseIdentifier:
1700/// ::= identifier
1701/// ::= string
1702bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001703 // The assembler has relaxed rules for accepting identifiers, in particular we
1704 // allow things like '.globl $foo', which would normally be separate
1705 // tokens. At this level, we have already lexed so we cannot (currently)
1706 // handle this as a context dependent token, instead we detect adjacent tokens
1707 // and return the combined identifier.
1708 if (Lexer.is(AsmToken::Dollar)) {
1709 SMLoc DollarLoc = getLexer().getLoc();
1710
1711 // Consume the dollar sign, and check for a following identifier.
1712 Lex();
1713 if (Lexer.isNot(AsmToken::Identifier))
1714 return true;
1715
1716 // We have a '$' followed by an identifier, make sure they are adjacent.
1717 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1718 return true;
1719
1720 // Construct the joined identifier and consume the token.
1721 Res = StringRef(DollarLoc.getPointer(),
1722 getTok().getIdentifier().size() + 1);
1723 Lex();
1724 return false;
1725 }
1726
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001727 if (Lexer.isNot(AsmToken::Identifier) &&
1728 Lexer.isNot(AsmToken::String))
1729 return true;
1730
Sean Callanan18b83232010-01-19 21:44:56 +00001731 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001732
Sean Callanan79ed1a82010-01-19 20:22:31 +00001733 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001734
1735 return false;
1736}
1737
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001738/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001739/// ::= .equ identifier ',' expression
1740/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001741/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001742bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001743 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001744
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001745 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001746 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001747
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001748 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001749 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001750 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001751
Nico Weber4c4c7322011-01-28 03:04:41 +00001752 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001753}
1754
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001755bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001756 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001757
1758 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001759 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001760 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1761 if (Str[i] != '\\') {
1762 Data += Str[i];
1763 continue;
1764 }
1765
1766 // Recognize escaped characters. Note that this escape semantics currently
1767 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1768 ++i;
1769 if (i == e)
1770 return TokError("unexpected backslash at end of string");
1771
1772 // Recognize octal sequences.
1773 if ((unsigned) (Str[i] - '0') <= 7) {
1774 // Consume up to three octal characters.
1775 unsigned Value = Str[i] - '0';
1776
1777 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1778 ++i;
1779 Value = Value * 8 + (Str[i] - '0');
1780
1781 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1782 ++i;
1783 Value = Value * 8 + (Str[i] - '0');
1784 }
1785 }
1786
1787 if (Value > 255)
1788 return TokError("invalid octal escape sequence (out of range)");
1789
1790 Data += (unsigned char) Value;
1791 continue;
1792 }
1793
1794 // Otherwise recognize individual escapes.
1795 switch (Str[i]) {
1796 default:
1797 // Just reject invalid escape sequences for now.
1798 return TokError("invalid escape sequence (unrecognized character)");
1799
1800 case 'b': Data += '\b'; break;
1801 case 'f': Data += '\f'; break;
1802 case 'n': Data += '\n'; break;
1803 case 'r': Data += '\r'; break;
1804 case 't': Data += '\t'; break;
1805 case '"': Data += '"'; break;
1806 case '\\': Data += '\\'; break;
1807 }
1808 }
1809
1810 return false;
1811}
1812
Daniel Dunbara0d14262009-06-24 23:30:00 +00001813/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001814/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1815bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001816 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001817 CheckForValidSection();
1818
Daniel Dunbara0d14262009-06-24 23:30:00 +00001819 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001820 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001821 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001822
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001823 std::string Data;
1824 if (ParseEscapedString(Data))
1825 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001826
1827 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001828 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001829 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1830
Sean Callanan79ed1a82010-01-19 20:22:31 +00001831 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001832
1833 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001834 break;
1835
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001836 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001837 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001838 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001839 }
1840 }
1841
Sean Callanan79ed1a82010-01-19 20:22:31 +00001842 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001843 return false;
1844}
1845
1846/// ParseDirectiveValue
1847/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1848bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001849 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001850 CheckForValidSection();
1851
Daniel Dunbara0d14262009-06-24 23:30:00 +00001852 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001853 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001854 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001855 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001856 return true;
1857
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001858 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001859 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1860 assert(Size <= 8 && "Invalid size");
1861 uint64_t IntValue = MCE->getValue();
1862 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1863 return Error(ExprLoc, "literal value out of range for directive");
1864 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1865 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001866 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001867
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001868 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001869 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001870
Daniel Dunbara0d14262009-06-24 23:30:00 +00001871 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001872 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001873 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001874 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001875 }
1876 }
1877
Sean Callanan79ed1a82010-01-19 20:22:31 +00001878 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001879 return false;
1880}
1881
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001882/// ParseDirectiveRealValue
1883/// ::= (.single | .double) [ expression (, expression)* ]
1884bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1885 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1886 CheckForValidSection();
1887
1888 for (;;) {
1889 // We don't truly support arithmetic on floating point expressions, so we
1890 // have to manually parse unary prefixes.
1891 bool IsNeg = false;
1892 if (getLexer().is(AsmToken::Minus)) {
1893 Lex();
1894 IsNeg = true;
1895 } else if (getLexer().is(AsmToken::Plus))
1896 Lex();
1897
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001898 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001899 getLexer().isNot(AsmToken::Real) &&
1900 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001901 return TokError("unexpected token in directive");
1902
1903 // Convert to an APFloat.
1904 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001905 StringRef IDVal = getTok().getString();
1906 if (getLexer().is(AsmToken::Identifier)) {
1907 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1908 Value = APFloat::getInf(Semantics);
1909 else if (!IDVal.compare_lower("nan"))
1910 Value = APFloat::getNaN(Semantics, false, ~0);
1911 else
1912 return TokError("invalid floating point literal");
1913 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001914 APFloat::opInvalidOp)
1915 return TokError("invalid floating point literal");
1916 if (IsNeg)
1917 Value.changeSign();
1918
1919 // Consume the numeric token.
1920 Lex();
1921
1922 // Emit the value as an integer.
1923 APInt AsInt = Value.bitcastToAPInt();
1924 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1925 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1926
1927 if (getLexer().is(AsmToken::EndOfStatement))
1928 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001929
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001930 if (getLexer().isNot(AsmToken::Comma))
1931 return TokError("unexpected token in directive");
1932 Lex();
1933 }
1934 }
1935
1936 Lex();
1937 return false;
1938}
1939
Daniel Dunbara0d14262009-06-24 23:30:00 +00001940/// ParseDirectiveSpace
1941/// ::= .space expression [ , expression ]
1942bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001943 CheckForValidSection();
1944
Daniel Dunbara0d14262009-06-24 23:30:00 +00001945 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001946 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001947 return true;
1948
1949 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001950 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1951 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001952 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001953 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001954
Daniel Dunbar475839e2009-06-29 20:37:27 +00001955 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001956 return true;
1957
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001958 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001959 return TokError("unexpected token in '.space' directive");
1960 }
1961
Sean Callanan79ed1a82010-01-19 20:22:31 +00001962 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001963
1964 if (NumBytes <= 0)
1965 return TokError("invalid number of bytes in '.space' directive");
1966
1967 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001968 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001969
1970 return false;
1971}
1972
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001973/// ParseDirectiveZero
1974/// ::= .zero expression
1975bool AsmParser::ParseDirectiveZero() {
1976 CheckForValidSection();
1977
1978 int64_t NumBytes;
1979 if (ParseAbsoluteExpression(NumBytes))
1980 return true;
1981
Rafael Espindolae452b172010-10-05 19:42:57 +00001982 int64_t Val = 0;
1983 if (getLexer().is(AsmToken::Comma)) {
1984 Lex();
1985 if (ParseAbsoluteExpression(Val))
1986 return true;
1987 }
1988
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001989 if (getLexer().isNot(AsmToken::EndOfStatement))
1990 return TokError("unexpected token in '.zero' directive");
1991
1992 Lex();
1993
Rafael Espindolae452b172010-10-05 19:42:57 +00001994 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001995
1996 return false;
1997}
1998
Daniel Dunbara0d14262009-06-24 23:30:00 +00001999/// ParseDirectiveFill
2000/// ::= .fill expression , expression , expression
2001bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002002 CheckForValidSection();
2003
Daniel Dunbara0d14262009-06-24 23:30:00 +00002004 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002005 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002006 return true;
2007
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002008 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002009 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002010 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002011
Daniel Dunbara0d14262009-06-24 23:30:00 +00002012 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002013 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002014 return true;
2015
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002016 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002017 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002018 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002019
Daniel Dunbara0d14262009-06-24 23:30:00 +00002020 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002021 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002022 return true;
2023
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002024 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002025 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002026
Sean Callanan79ed1a82010-01-19 20:22:31 +00002027 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002028
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002029 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2030 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002031
2032 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002033 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002034
2035 return false;
2036}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002037
2038/// ParseDirectiveOrg
2039/// ::= .org expression [ , expression ]
2040bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002041 CheckForValidSection();
2042
Daniel Dunbar821e3332009-08-31 08:09:28 +00002043 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002044 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002045 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002046 return true;
2047
2048 // Parse optional fill expression.
2049 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002050 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2051 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002052 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002053 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002054
Daniel Dunbar475839e2009-06-29 20:37:27 +00002055 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002056 return true;
2057
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002058 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002059 return TokError("unexpected token in '.org' directive");
2060 }
2061
Sean Callanan79ed1a82010-01-19 20:22:31 +00002062 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002063
Jim Grosbachebd4c052012-01-27 00:37:08 +00002064 // Only limited forms of relocatable expressions are accepted here, it
2065 // has to be relative to the current section. The streamer will return
2066 // 'true' if the expression wasn't evaluatable.
2067 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2068 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002069
2070 return false;
2071}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002072
2073/// ParseDirectiveAlign
2074/// ::= {.align, ...} expression [ , expression [ , expression ]]
2075bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002076 CheckForValidSection();
2077
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002078 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002079 int64_t Alignment;
2080 if (ParseAbsoluteExpression(Alignment))
2081 return true;
2082
2083 SMLoc MaxBytesLoc;
2084 bool HasFillExpr = false;
2085 int64_t FillExpr = 0;
2086 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002087 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2088 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002089 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002090 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002091
2092 // The fill expression can be omitted while specifying a maximum number of
2093 // alignment bytes, e.g:
2094 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002095 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002096 HasFillExpr = true;
2097 if (ParseAbsoluteExpression(FillExpr))
2098 return true;
2099 }
2100
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002101 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2102 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002103 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002104 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002105
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002106 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002107 if (ParseAbsoluteExpression(MaxBytesToFill))
2108 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002109
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002110 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002111 return TokError("unexpected token in directive");
2112 }
2113 }
2114
Sean Callanan79ed1a82010-01-19 20:22:31 +00002115 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002116
Daniel Dunbar648ac512010-05-17 21:54:30 +00002117 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002118 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002119
2120 // Compute alignment in bytes.
2121 if (IsPow2) {
2122 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002123 if (Alignment >= 32) {
2124 Error(AlignmentLoc, "invalid alignment value");
2125 Alignment = 31;
2126 }
2127
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002128 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002129 }
2130
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002131 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002132 if (MaxBytesLoc.isValid()) {
2133 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002134 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2135 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002136 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002137 }
2138
2139 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002140 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2141 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002142 MaxBytesToFill = 0;
2143 }
2144 }
2145
Daniel Dunbar648ac512010-05-17 21:54:30 +00002146 // Check whether we should use optimal code alignment for this .align
2147 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002148 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002149 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2150 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002151 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002152 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002153 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002154 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2155 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002156 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002157
2158 return false;
2159}
2160
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002161/// ParseDirectiveSymbolAttribute
2162/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002163bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002164 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002165 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002166 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002167 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002168
2169 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002170 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002171
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002172 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002173
Jim Grosbach10ec6502011-09-15 17:56:49 +00002174 // Assembler local symbols don't make any sense here. Complain loudly.
2175 if (Sym->isTemporary())
2176 return Error(Loc, "non-local symbol required in directive");
2177
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002178 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002179
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002180 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002181 break;
2182
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002183 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002184 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002185 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002186 }
2187 }
2188
Sean Callanan79ed1a82010-01-19 20:22:31 +00002189 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002190 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002191}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002192
2193/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002194/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2195bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002196 CheckForValidSection();
2197
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002198 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002199 StringRef Name;
2200 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002201 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002202
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002203 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002204 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002205
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002206 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002207 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002208 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002209
2210 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002211 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002212 if (ParseAbsoluteExpression(Size))
2213 return true;
2214
2215 int64_t Pow2Alignment = 0;
2216 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002217 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002218 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002219 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002220 if (ParseAbsoluteExpression(Pow2Alignment))
2221 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002222
Chris Lattner258281d2010-01-19 06:22:22 +00002223 // If this target takes alignments in bytes (not log) validate and convert.
2224 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2225 if (!isPowerOf2_64(Pow2Alignment))
2226 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2227 Pow2Alignment = Log2_64(Pow2Alignment);
2228 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002229 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002230
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002231 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002232 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002233
Sean Callanan79ed1a82010-01-19 20:22:31 +00002234 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002235
Chris Lattner1fc3d752009-07-09 17:25:12 +00002236 // NOTE: a size of zero for a .comm should create a undefined symbol
2237 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002238 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002239 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2240 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002241
Eric Christopherc260a3e2010-05-14 01:38:54 +00002242 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002243 // may internally end up wanting an alignment in bytes.
2244 // FIXME: Diagnose overflow.
2245 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002246 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2247 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002248
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002249 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002250 return Error(IDLoc, "invalid symbol redefinition");
2251
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002252 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002253 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002254 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002255 getStreamer().EmitZerofill(Ctx.getMachOSection(
2256 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2257 0, SectionKind::getBSS()),
2258 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002259 return false;
2260 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002261
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002262 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002263 return false;
2264}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002265
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002266/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002267/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002268bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002269 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002270 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002271
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002272 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002273 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002274 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002275
Sean Callanan79ed1a82010-01-19 20:22:31 +00002276 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002277
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002278 if (Str.empty())
2279 Error(Loc, ".abort detected. Assembly stopping.");
2280 else
2281 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002282 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002283
2284 return false;
2285}
Kevin Enderby71148242009-07-14 21:35:03 +00002286
Kevin Enderby1f049b22009-07-14 23:21:55 +00002287/// ParseDirectiveInclude
2288/// ::= .include "filename"
2289bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002290 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002291 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002292
Sean Callanan18b83232010-01-19 21:44:56 +00002293 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002294 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002295 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002296
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002297 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002298 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002299
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002300 // Strip the quotes.
2301 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002302
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002303 // Attempt to switch the lexer to the included file before consuming the end
2304 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002305 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002306 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002307 return true;
2308 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002309
2310 return false;
2311}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002312
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002313/// ParseDirectiveIncbin
2314/// ::= .incbin "filename"
2315bool AsmParser::ParseDirectiveIncbin() {
2316 if (getLexer().isNot(AsmToken::String))
2317 return TokError("expected string in '.incbin' directive");
2318
2319 std::string Filename = getTok().getString();
2320 SMLoc IncbinLoc = getLexer().getLoc();
2321 Lex();
2322
2323 if (getLexer().isNot(AsmToken::EndOfStatement))
2324 return TokError("unexpected token in '.incbin' directive");
2325
2326 // Strip the quotes.
2327 Filename = Filename.substr(1, Filename.size()-2);
2328
2329 // Attempt to process the included file.
2330 if (ProcessIncbinFile(Filename)) {
2331 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2332 return true;
2333 }
2334
2335 return false;
2336}
2337
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002338/// ParseDirectiveIf
2339/// ::= .if expression
2340bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002341 TheCondStack.push_back(TheCondState);
2342 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002343 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002344 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002345 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002346 int64_t ExprValue;
2347 if (ParseAbsoluteExpression(ExprValue))
2348 return true;
2349
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002350 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002351 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002352
Sean Callanan79ed1a82010-01-19 20:22:31 +00002353 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002354
2355 TheCondState.CondMet = ExprValue;
2356 TheCondState.Ignore = !TheCondState.CondMet;
2357 }
2358
2359 return false;
2360}
2361
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002362/// ParseDirectiveIfb
2363/// ::= .ifb string
2364bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2365 TheCondStack.push_back(TheCondState);
2366 TheCondState.TheCond = AsmCond::IfCond;
2367
Benjamin Kramer29739e72012-05-12 16:52:21 +00002368 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002369 EatToEndOfStatement();
2370 } else {
2371 StringRef Str = ParseStringToEndOfStatement();
2372
2373 if (getLexer().isNot(AsmToken::EndOfStatement))
2374 return TokError("unexpected token in '.ifb' directive");
2375
2376 Lex();
2377
2378 TheCondState.CondMet = ExpectBlank == Str.empty();
2379 TheCondState.Ignore = !TheCondState.CondMet;
2380 }
2381
2382 return false;
2383}
2384
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002385/// ParseDirectiveIfc
2386/// ::= .ifc string1, string2
2387bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2388 TheCondStack.push_back(TheCondState);
2389 TheCondState.TheCond = AsmCond::IfCond;
2390
Benjamin Kramer29739e72012-05-12 16:52:21 +00002391 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002392 EatToEndOfStatement();
2393 } else {
2394 StringRef Str1 = ParseStringToComma();
2395
2396 if (getLexer().isNot(AsmToken::Comma))
2397 return TokError("unexpected token in '.ifc' directive");
2398
2399 Lex();
2400
2401 StringRef Str2 = ParseStringToEndOfStatement();
2402
2403 if (getLexer().isNot(AsmToken::EndOfStatement))
2404 return TokError("unexpected token in '.ifc' directive");
2405
2406 Lex();
2407
2408 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2409 TheCondState.Ignore = !TheCondState.CondMet;
2410 }
2411
2412 return false;
2413}
2414
2415/// ParseDirectiveIfdef
2416/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002417bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2418 StringRef Name;
2419 TheCondStack.push_back(TheCondState);
2420 TheCondState.TheCond = AsmCond::IfCond;
2421
2422 if (TheCondState.Ignore) {
2423 EatToEndOfStatement();
2424 } else {
2425 if (ParseIdentifier(Name))
2426 return TokError("expected identifier after '.ifdef'");
2427
2428 Lex();
2429
2430 MCSymbol *Sym = getContext().LookupSymbol(Name);
2431
2432 if (expect_defined)
2433 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2434 else
2435 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2436 TheCondState.Ignore = !TheCondState.CondMet;
2437 }
2438
2439 return false;
2440}
2441
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002442/// ParseDirectiveElseIf
2443/// ::= .elseif expression
2444bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2445 if (TheCondState.TheCond != AsmCond::IfCond &&
2446 TheCondState.TheCond != AsmCond::ElseIfCond)
2447 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2448 " an .elseif");
2449 TheCondState.TheCond = AsmCond::ElseIfCond;
2450
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002451 bool LastIgnoreState = false;
2452 if (!TheCondStack.empty())
2453 LastIgnoreState = TheCondStack.back().Ignore;
2454 if (LastIgnoreState || TheCondState.CondMet) {
2455 TheCondState.Ignore = true;
2456 EatToEndOfStatement();
2457 }
2458 else {
2459 int64_t ExprValue;
2460 if (ParseAbsoluteExpression(ExprValue))
2461 return true;
2462
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002463 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002464 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002465
Sean Callanan79ed1a82010-01-19 20:22:31 +00002466 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002467 TheCondState.CondMet = ExprValue;
2468 TheCondState.Ignore = !TheCondState.CondMet;
2469 }
2470
2471 return false;
2472}
2473
2474/// ParseDirectiveElse
2475/// ::= .else
2476bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002477 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002478 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002479
Sean Callanan79ed1a82010-01-19 20:22:31 +00002480 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002481
2482 if (TheCondState.TheCond != AsmCond::IfCond &&
2483 TheCondState.TheCond != AsmCond::ElseIfCond)
2484 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2485 ".elseif");
2486 TheCondState.TheCond = AsmCond::ElseCond;
2487 bool LastIgnoreState = false;
2488 if (!TheCondStack.empty())
2489 LastIgnoreState = TheCondStack.back().Ignore;
2490 if (LastIgnoreState || TheCondState.CondMet)
2491 TheCondState.Ignore = true;
2492 else
2493 TheCondState.Ignore = false;
2494
2495 return false;
2496}
2497
2498/// ParseDirectiveEndIf
2499/// ::= .endif
2500bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002501 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002502 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002503
Sean Callanan79ed1a82010-01-19 20:22:31 +00002504 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002505
2506 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2507 TheCondStack.empty())
2508 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2509 ".else");
2510 if (!TheCondStack.empty()) {
2511 TheCondState = TheCondStack.back();
2512 TheCondStack.pop_back();
2513 }
2514
2515 return false;
2516}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002517
2518/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002519/// ::= .file [number] filename
2520/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002521bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002522 // FIXME: I'm not sure what this is.
2523 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002524 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002525 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002526 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002527 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002528
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002529 if (FileNumber < 1)
2530 return TokError("file number less than one");
2531 }
2532
Daniel Dunbareceec052010-07-12 17:45:27 +00002533 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002534 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002535
Nick Lewycky44d798d2011-10-17 23:05:28 +00002536 // Usually the directory and filename together, otherwise just the directory.
2537 StringRef Path = getTok().getString();
2538 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002539 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002540
Nick Lewycky44d798d2011-10-17 23:05:28 +00002541 StringRef Directory;
2542 StringRef Filename;
2543 if (getLexer().is(AsmToken::String)) {
2544 if (FileNumber == -1)
2545 return TokError("explicit path specified, but no file number");
2546 Filename = getTok().getString();
2547 Filename = Filename.substr(1, Filename.size()-2);
2548 Directory = Path;
2549 Lex();
2550 } else {
2551 Filename = Path;
2552 }
2553
Daniel Dunbareceec052010-07-12 17:45:27 +00002554 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002555 return TokError("unexpected token in '.file' directive");
2556
Chris Lattnerd32e8032010-01-25 19:02:58 +00002557 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002558 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002559 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002560 if (getContext().getGenDwarfForAssembly() == true)
2561 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2562 "used to generate dwarf debug info for assembly code");
2563
Nick Lewycky44d798d2011-10-17 23:05:28 +00002564 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002565 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002566 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002567
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002568 return false;
2569}
2570
2571/// ParseDirectiveLine
2572/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002573bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002574 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2575 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002576 return TokError("unexpected token in '.line' directive");
2577
Sean Callanan18b83232010-01-19 21:44:56 +00002578 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002579 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002580 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002581
2582 // FIXME: Do something with the .line.
2583 }
2584
Daniel Dunbareceec052010-07-12 17:45:27 +00002585 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002586 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002587
2588 return false;
2589}
2590
2591
2592/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002593/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002594/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2595/// The first number is a file number, must have been previously assigned with
2596/// a .file directive, the second number is the line number and optionally the
2597/// third number is a column position (zero if not specified). The remaining
2598/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002599bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002600
Daniel Dunbareceec052010-07-12 17:45:27 +00002601 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002602 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002603 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002604 if (FileNumber < 1)
2605 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002606 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002607 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002608 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002609
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002610 int64_t LineNumber = 0;
2611 if (getLexer().is(AsmToken::Integer)) {
2612 LineNumber = getTok().getIntVal();
2613 if (LineNumber < 1)
2614 return TokError("line number less than one in '.loc' directive");
2615 Lex();
2616 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002617
2618 int64_t ColumnPos = 0;
2619 if (getLexer().is(AsmToken::Integer)) {
2620 ColumnPos = getTok().getIntVal();
2621 if (ColumnPos < 0)
2622 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002623 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002624 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002625
Kevin Enderbyc0957932010-09-30 16:52:03 +00002626 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002627 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002628 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002629 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2630 for (;;) {
2631 if (getLexer().is(AsmToken::EndOfStatement))
2632 break;
2633
2634 StringRef Name;
2635 SMLoc Loc = getTok().getLoc();
2636 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002637 return TokError("unexpected token in '.loc' directive");
2638
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002639 if (Name == "basic_block")
2640 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2641 else if (Name == "prologue_end")
2642 Flags |= DWARF2_FLAG_PROLOGUE_END;
2643 else if (Name == "epilogue_begin")
2644 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2645 else if (Name == "is_stmt") {
2646 SMLoc Loc = getTok().getLoc();
2647 const MCExpr *Value;
2648 if (getParser().ParseExpression(Value))
2649 return true;
2650 // The expression must be the constant 0 or 1.
2651 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2652 int Value = MCE->getValue();
2653 if (Value == 0)
2654 Flags &= ~DWARF2_FLAG_IS_STMT;
2655 else if (Value == 1)
2656 Flags |= DWARF2_FLAG_IS_STMT;
2657 else
2658 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002659 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002660 else {
2661 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2662 }
2663 }
2664 else if (Name == "isa") {
2665 SMLoc Loc = getTok().getLoc();
2666 const MCExpr *Value;
2667 if (getParser().ParseExpression(Value))
2668 return true;
2669 // The expression must be a constant greater or equal to 0.
2670 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2671 int Value = MCE->getValue();
2672 if (Value < 0)
2673 return Error(Loc, "isa number less than zero");
2674 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002675 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002676 else {
2677 return Error(Loc, "isa number not a constant value");
2678 }
2679 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002680 else if (Name == "discriminator") {
2681 if (getParser().ParseAbsoluteExpression(Discriminator))
2682 return true;
2683 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002684 else {
2685 return Error(Loc, "unknown sub-directive in '.loc' directive");
2686 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002687
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002688 if (getLexer().is(AsmToken::EndOfStatement))
2689 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002690 }
2691 }
2692
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002693 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002694 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002695
2696 return false;
2697}
2698
Daniel Dunbar138abae2010-10-16 04:56:42 +00002699/// ParseDirectiveStabs
2700/// ::= .stabs string, number, number, number
2701bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2702 SMLoc DirectiveLoc) {
2703 return TokError("unsupported directive '" + Directive + "'");
2704}
2705
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002706/// ParseDirectiveCFISections
2707/// ::= .cfi_sections section [, section]
2708bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2709 SMLoc DirectiveLoc) {
2710 StringRef Name;
2711 bool EH = false;
2712 bool Debug = false;
2713
2714 if (getParser().ParseIdentifier(Name))
2715 return TokError("Expected an identifier");
2716
2717 if (Name == ".eh_frame")
2718 EH = true;
2719 else if (Name == ".debug_frame")
2720 Debug = true;
2721
2722 if (getLexer().is(AsmToken::Comma)) {
2723 Lex();
2724
2725 if (getParser().ParseIdentifier(Name))
2726 return TokError("Expected an identifier");
2727
2728 if (Name == ".eh_frame")
2729 EH = true;
2730 else if (Name == ".debug_frame")
2731 Debug = true;
2732 }
2733
2734 getStreamer().EmitCFISections(EH, Debug);
2735
2736 return false;
2737}
2738
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002739/// ParseDirectiveCFIStartProc
2740/// ::= .cfi_startproc
2741bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2742 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002743 getStreamer().EmitCFIStartProc();
2744 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002745}
2746
2747/// ParseDirectiveCFIEndProc
2748/// ::= .cfi_endproc
2749bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002750 getStreamer().EmitCFIEndProc();
2751 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002752}
2753
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002754/// ParseRegisterOrRegisterNumber - parse register name or number.
2755bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2756 SMLoc DirectiveLoc) {
2757 unsigned RegNo;
2758
Jim Grosbach6f888a82011-06-02 17:14:04 +00002759 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002760 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2761 DirectiveLoc))
2762 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002763 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002764 } else
2765 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002766
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002767 return false;
2768}
2769
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002770/// ParseDirectiveCFIDefCfa
2771/// ::= .cfi_def_cfa register, offset
2772bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2773 SMLoc DirectiveLoc) {
2774 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002775 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002776 return true;
2777
2778 if (getLexer().isNot(AsmToken::Comma))
2779 return TokError("unexpected token in directive");
2780 Lex();
2781
2782 int64_t Offset = 0;
2783 if (getParser().ParseAbsoluteExpression(Offset))
2784 return true;
2785
Rafael Espindola066c2f42011-04-12 23:59:07 +00002786 getStreamer().EmitCFIDefCfa(Register, Offset);
2787 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002788}
2789
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002790/// ParseDirectiveCFIDefCfaOffset
2791/// ::= .cfi_def_cfa_offset offset
2792bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2793 SMLoc DirectiveLoc) {
2794 int64_t Offset = 0;
2795 if (getParser().ParseAbsoluteExpression(Offset))
2796 return true;
2797
Rafael Espindola066c2f42011-04-12 23:59:07 +00002798 getStreamer().EmitCFIDefCfaOffset(Offset);
2799 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002800}
2801
2802/// ParseDirectiveCFIAdjustCfaOffset
2803/// ::= .cfi_adjust_cfa_offset adjustment
2804bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2805 SMLoc DirectiveLoc) {
2806 int64_t Adjustment = 0;
2807 if (getParser().ParseAbsoluteExpression(Adjustment))
2808 return true;
2809
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002810 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2811 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002812}
2813
2814/// ParseDirectiveCFIDefCfaRegister
2815/// ::= .cfi_def_cfa_register register
2816bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2817 SMLoc DirectiveLoc) {
2818 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002819 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002820 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002821
Rafael Espindola066c2f42011-04-12 23:59:07 +00002822 getStreamer().EmitCFIDefCfaRegister(Register);
2823 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002824}
2825
2826/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002827/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002828bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2829 int64_t Register = 0;
2830 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002831
2832 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002833 return true;
2834
2835 if (getLexer().isNot(AsmToken::Comma))
2836 return TokError("unexpected token in directive");
2837 Lex();
2838
2839 if (getParser().ParseAbsoluteExpression(Offset))
2840 return true;
2841
Rafael Espindola066c2f42011-04-12 23:59:07 +00002842 getStreamer().EmitCFIOffset(Register, Offset);
2843 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002844}
2845
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002846/// ParseDirectiveCFIRelOffset
2847/// ::= .cfi_rel_offset register, offset
2848bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2849 SMLoc DirectiveLoc) {
2850 int64_t Register = 0;
2851
2852 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2853 return true;
2854
2855 if (getLexer().isNot(AsmToken::Comma))
2856 return TokError("unexpected token in directive");
2857 Lex();
2858
2859 int64_t Offset = 0;
2860 if (getParser().ParseAbsoluteExpression(Offset))
2861 return true;
2862
Rafael Espindola25f492e2011-04-12 16:12:03 +00002863 getStreamer().EmitCFIRelOffset(Register, Offset);
2864 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002865}
2866
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002867static bool isValidEncoding(int64_t Encoding) {
2868 if (Encoding & ~0xff)
2869 return false;
2870
2871 if (Encoding == dwarf::DW_EH_PE_omit)
2872 return true;
2873
2874 const unsigned Format = Encoding & 0xf;
2875 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2876 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2877 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2878 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2879 return false;
2880
Rafael Espindolacaf11582010-12-29 04:31:26 +00002881 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002882 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002883 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002884 return false;
2885
2886 return true;
2887}
2888
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002889/// ParseDirectiveCFIPersonalityOrLsda
2890/// ::= .cfi_personality encoding, [symbol_name]
2891/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002892bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002893 SMLoc DirectiveLoc) {
2894 int64_t Encoding = 0;
2895 if (getParser().ParseAbsoluteExpression(Encoding))
2896 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002897 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002898 return false;
2899
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002900 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002901 return TokError("unsupported encoding.");
2902
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002903 if (getLexer().isNot(AsmToken::Comma))
2904 return TokError("unexpected token in directive");
2905 Lex();
2906
2907 StringRef Name;
2908 if (getParser().ParseIdentifier(Name))
2909 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002910
2911 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2912
2913 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002914 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002915 else {
2916 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002917 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002918 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002919 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002920}
2921
Rafael Espindolafe024d02010-12-28 18:36:23 +00002922/// ParseDirectiveCFIRememberState
2923/// ::= .cfi_remember_state
2924bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2925 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002926 getStreamer().EmitCFIRememberState();
2927 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002928}
2929
2930/// ParseDirectiveCFIRestoreState
2931/// ::= .cfi_remember_state
2932bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2933 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002934 getStreamer().EmitCFIRestoreState();
2935 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002936}
2937
Rafael Espindolac5754392011-04-12 15:31:05 +00002938/// ParseDirectiveCFISameValue
2939/// ::= .cfi_same_value register
2940bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2941 SMLoc DirectiveLoc) {
2942 int64_t Register = 0;
2943
2944 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2945 return true;
2946
2947 getStreamer().EmitCFISameValue(Register);
2948
2949 return false;
2950}
2951
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00002952/// ParseDirectiveCFIRestore
2953/// ::= .cfi_restore register
2954bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
2955 SMLoc DirectiveLoc) {
2956 int64_t Register = 0;
2957 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2958 return true;
2959
2960 getStreamer().EmitCFIRestore(Register);
2961
2962 return false;
2963}
2964
Rafael Espindola6f0b1812011-12-29 20:24:47 +00002965/// ParseDirectiveCFIEscape
2966/// ::= .cfi_escape expression[,...]
2967bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
2968 SMLoc DirectiveLoc) {
2969 std::string Values;
2970 int64_t CurrValue;
2971 if (getParser().ParseAbsoluteExpression(CurrValue))
2972 return true;
2973
2974 Values.push_back((uint8_t)CurrValue);
2975
2976 while (getLexer().is(AsmToken::Comma)) {
2977 Lex();
2978
2979 if (getParser().ParseAbsoluteExpression(CurrValue))
2980 return true;
2981
2982 Values.push_back((uint8_t)CurrValue);
2983 }
2984
2985 getStreamer().EmitCFIEscape(Values);
2986 return false;
2987}
2988
Rafael Espindola16d7d432012-01-23 21:51:52 +00002989/// ParseDirectiveCFISignalFrame
2990/// ::= .cfi_signal_frame
2991bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
2992 SMLoc DirectiveLoc) {
2993 if (getLexer().isNot(AsmToken::EndOfStatement))
2994 return Error(getLexer().getLoc(),
2995 "unexpected token in '" + Directive + "' directive");
2996
2997 getStreamer().EmitCFISignalFrame();
2998
2999 return false;
3000}
3001
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003002/// ParseDirectiveMacrosOnOff
3003/// ::= .macros_on
3004/// ::= .macros_off
3005bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3006 SMLoc DirectiveLoc) {
3007 if (getLexer().isNot(AsmToken::EndOfStatement))
3008 return Error(getLexer().getLoc(),
3009 "unexpected token in '" + Directive + "' directive");
3010
3011 getParser().MacrosEnabled = Directive == ".macros_on";
3012
3013 return false;
3014}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003015
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003016/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003017/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003018bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3019 SMLoc DirectiveLoc) {
3020 StringRef Name;
3021 if (getParser().ParseIdentifier(Name))
3022 return TokError("expected identifier in directive");
3023
Rafael Espindola65366442011-06-05 02:43:45 +00003024 std::vector<StringRef> Parameters;
3025 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3026 for(;;) {
3027 StringRef Parameter;
3028 if (getParser().ParseIdentifier(Parameter))
3029 return TokError("expected identifier in directive");
3030 Parameters.push_back(Parameter);
3031
3032 if (getLexer().isNot(AsmToken::Comma))
3033 break;
3034 Lex();
3035 }
3036 }
3037
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003038 if (getLexer().isNot(AsmToken::EndOfStatement))
3039 return TokError("unexpected token in '.macro' directive");
3040
3041 // Eat the end of statement.
3042 Lex();
3043
3044 AsmToken EndToken, StartToken = getTok();
3045
3046 // Lex the macro definition.
3047 for (;;) {
3048 // Check whether we have reached the end of the file.
3049 if (getLexer().is(AsmToken::Eof))
3050 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3051
3052 // Otherwise, check whether we have reach the .endmacro.
3053 if (getLexer().is(AsmToken::Identifier) &&
3054 (getTok().getIdentifier() == ".endm" ||
3055 getTok().getIdentifier() == ".endmacro")) {
3056 EndToken = getTok();
3057 Lex();
3058 if (getLexer().isNot(AsmToken::EndOfStatement))
3059 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3060 "' directive");
3061 break;
3062 }
3063
3064 // Otherwise, scan til the end of the statement.
3065 getParser().EatToEndOfStatement();
3066 }
3067
3068 if (getParser().MacroMap.lookup(Name)) {
3069 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3070 }
3071
3072 const char *BodyStart = StartToken.getLoc().getPointer();
3073 const char *BodyEnd = EndToken.getLoc().getPointer();
3074 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003075 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003076 return false;
3077}
3078
3079/// ParseDirectiveEndMacro
3080/// ::= .endm
3081/// ::= .endmacro
3082bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
3083 SMLoc DirectiveLoc) {
3084 if (getLexer().isNot(AsmToken::EndOfStatement))
3085 return TokError("unexpected token in '" + Directive + "' directive");
3086
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003087 // If we are inside a macro instantiation, terminate the current
3088 // instantiation.
3089 if (!getParser().ActiveMacros.empty()) {
3090 getParser().HandleMacroExit();
3091 return false;
3092 }
3093
3094 // Otherwise, this .endmacro is a stray entry in the file; well formed
3095 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003096 return TokError("unexpected '" + Directive + "' in file, "
3097 "no current macro definition");
3098}
3099
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003100/// ParseDirectivePurgeMacro
3101/// ::= .purgem
3102bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3103 SMLoc DirectiveLoc) {
3104 StringRef Name;
3105 if (getParser().ParseIdentifier(Name))
3106 return TokError("expected identifier in '.purgem' directive");
3107
3108 if (getLexer().isNot(AsmToken::EndOfStatement))
3109 return TokError("unexpected token in '.purgem' directive");
3110
3111 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3112 if (I == getParser().MacroMap.end())
3113 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3114
3115 // Undefine the macro.
3116 delete I->getValue();
3117 getParser().MacroMap.erase(I);
3118 return false;
3119}
3120
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003121bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003122 getParser().CheckForValidSection();
3123
3124 const MCExpr *Value;
3125
3126 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003127 return true;
3128
3129 if (getLexer().isNot(AsmToken::EndOfStatement))
3130 return TokError("unexpected token in directive");
3131
3132 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003133 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003134 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003135 getStreamer().EmitULEB128Value(Value);
3136
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003137 return false;
3138}
3139
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003140bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3141 const MCExpr *Value;
3142
3143 if (ParseExpression(Value))
3144 return true;
3145
3146 int64_t Count;
3147 if (!Value->EvaluateAsAbsolute(Count))
3148 return TokError("Cannot evaluate value");
3149
3150 if (Count < 0)
3151 return TokError("Count is negative");
3152
3153 AsmToken EndToken, StartToken = getTok();
3154 unsigned Nest = 1;
3155
3156 // Lex the macro definition.
3157 for (;;) {
3158 // Check whether we have reached the end of the file.
3159 if (getLexer().is(AsmToken::Eof))
3160 return Error(DirectiveLoc, "no matching '.endr' in definition");
3161
3162 // Chcek if we have a nested .rept.
3163 if (getLexer().is(AsmToken::Identifier) &&
3164 (getTok().getIdentifier() == ".rept")) {
3165 Nest++;
3166 EatToEndOfStatement();
3167 continue;
3168 }
3169
3170 // Otherwise, check whether we have reach the .endr.
3171 if (getLexer().is(AsmToken::Identifier) &&
3172 (getTok().getIdentifier() == ".endr")) {
3173 Nest--;
3174 if (Nest == 0) {
3175 EndToken = getTok();
3176 Lex();
3177 if (getLexer().isNot(AsmToken::EndOfStatement))
3178 return TokError("unexpected token in '.endr' directive");
3179 break;
3180 }
3181 }
3182
3183 // Otherwise, scan til the end of the statement.
3184 EatToEndOfStatement();
3185 }
3186
3187 const char *BodyStart = StartToken.getLoc().getPointer();
3188 const char *BodyEnd = EndToken.getLoc().getPointer();
3189 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3190
3191 SmallString<256> Buf;
3192 raw_svector_ostream OS(Buf);
3193 for (int i = 0; i < Count; i++)
3194 OS << Body;
3195 OS << ".endr\n";
3196
3197 MemoryBuffer *Instantiation =
3198 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3199
3200 CurBuffer = SrcMgr.AddNewSourceBuffer(Instantiation, SMLoc());
3201 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3202
3203 ActiveRept.push_back(getTok().getLoc());
3204
3205 return false;
3206}
3207
3208bool AsmParser::ParseDirectiveEndRept(SMLoc DirectiveLoc) {
3209 if (ActiveRept.empty())
3210 return TokError("unexpected '.endr' directive, no current .rept");
3211
3212 // The only .repl that should get here are the ones created by
3213 // ParseDirectiveRept.
3214 assert(getLexer().is(AsmToken::EndOfStatement));
3215
3216 JumpToLoc(ActiveRept.back());
3217 ActiveRept.pop_back();
3218 return false;
3219}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003220
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003221/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003222MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003223 MCContext &C, MCStreamer &Out,
3224 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003225 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003226}