blob: 109ce5b4f242323aee83289a5a0c3ad80516e8a1 [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.
48struct Macro {
49 StringRef Name;
50 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000051 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000052
53public:
Rafael Espindola65366442011-06-05 02:43:45 +000054 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
55 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000056};
57
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000058/// \brief Helper class for storing information about an active macro
59/// instantiation.
60struct MacroInstantiation {
61 /// The macro being instantiated.
62 const Macro *TheMacro;
63
64 /// The macro instantiation with substitutions.
65 MemoryBuffer *Instantiation;
66
67 /// The location of the instantiation.
68 SMLoc InstantiationLoc;
69
70 /// The location where parsing should resume upon instantiation completion.
71 SMLoc ExitLoc;
72
73public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000074 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000075 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000076};
77
Daniel Dunbaraef87e32010-07-18 18:31:38 +000078/// \brief The concrete assembly parser instance.
79class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000080 friend class GenericAsmParser;
81
Daniel Dunbaraef87e32010-07-18 18:31:38 +000082 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
83 void operator=(const AsmParser &); // DO NOT IMPLEMENT
84private:
85 AsmLexer Lexer;
86 MCContext &Ctx;
87 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000088 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000090 SourceMgr::DiagHandlerTy SavedDiagHandler;
91 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000092 MCAsmParserExtension *GenericParser;
93 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000094
Daniel Dunbaraef87e32010-07-18 18:31:38 +000095 /// This is the current buffer index we're lexing from as managed by the
96 /// SourceMgr object.
97 int CurBuffer;
98
99 AsmCond TheCondState;
100 std::vector<AsmCond> TheCondStack;
101
102 /// DirectiveMap - This is a table handlers for directives. Each handler is
103 /// invoked after the directive identifier is read and is responsible for
104 /// parsing and validating the rest of the directive. The handler is passed
105 /// in the directive name and the location of the directive keyword.
106 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000107
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000108 /// MacroMap - Map of currently defined macros.
109 StringMap<Macro*> MacroMap;
110
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000111 /// ActiveMacros - Stack of active macro instantiations.
112 std::vector<MacroInstantiation*> ActiveMacros;
113
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000114 /// Boolean tracking whether macro substitution is enabled.
115 unsigned MacrosEnabled : 1;
116
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000117 /// Flag tracking whether any errors have been encountered.
118 unsigned HadError : 1;
119
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000120 /// The values from the last parsed cpp hash file line comment if any.
121 StringRef CppHashFilename;
122 int64_t CppHashLineNumber;
123 SMLoc CppHashLoc;
124
Devang Patel0db58bf2012-01-31 18:14:05 +0000125 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
126 unsigned AssemblerDialect;
127
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000128public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000129 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000130 const MCAsmInfo &MAI);
131 ~AsmParser();
132
133 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
134
135 void AddDirectiveHandler(MCAsmParserExtension *Object,
136 StringRef Directive,
137 DirectiveHandler Handler) {
138 DirectiveMap[Directive] = std::make_pair(Object, Handler);
139 }
140
141public:
142 /// @name MCAsmParser Interface
143 /// {
144
145 virtual SourceMgr &getSourceManager() { return SrcMgr; }
146 virtual MCAsmLexer &getLexer() { return Lexer; }
147 virtual MCContext &getContext() { return Ctx; }
148 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000149 virtual unsigned getAssemblerDialect() {
150 if (AssemblerDialect == ~0U)
151 return MAI.getAssemblerDialect();
152 else
153 return AssemblerDialect;
154 }
155 virtual void setAssemblerDialect(unsigned i) {
156 AssemblerDialect = i;
157 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000158
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000159 virtual bool Warning(SMLoc L, const Twine &Msg,
160 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
161 virtual bool Error(SMLoc L, const Twine &Msg,
162 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163
164 const AsmToken &Lex();
165
166 bool ParseExpression(const MCExpr *&Res);
167 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
168 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
169 virtual bool ParseAbsoluteExpression(int64_t &Res);
170
171 /// }
172
173private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000174 void CheckForValidSection();
175
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000176 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000177 void EatToEndOfLine();
178 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000179
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000180 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000181 bool expandMacro(SmallString<256> &Buf, StringRef Body,
182 const std::vector<StringRef> &Parameters,
183 const std::vector<std::vector<AsmToken> > &A,
184 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000185 void HandleMacroExit();
186
187 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000188 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000189 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
190 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000191 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000192 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000193
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000194 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
195 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000196 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
197 /// This returns true on failure.
198 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000199
200 /// \brief Reset the current lexer position to that given by \arg Loc. The
201 /// current token is not set; clients should ensure Lex() is called
202 /// subsequently.
203 void JumpToLoc(SMLoc Loc);
204
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000205 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000206
207 /// \brief Parse up to the end of statement and a return the contents from the
208 /// current token until the end of the statement; the current token on exit
209 /// will be either the EndOfStatement or EOF.
210 StringRef ParseStringToEndOfStatement();
211
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000212 /// \brief Parse until the end of a statement or a comma is encountered,
213 /// return the contents from the current token up to the end or comma.
214 StringRef ParseStringToComma();
215
Nico Weber4c4c7322011-01-28 03:04:41 +0000216 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000217
218 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
219 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
220 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000221 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000222
223 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
224 /// and set \arg Res to the identifier contents.
225 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000226
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000227 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000228
229 // ".ascii", ".asciiz", ".string"
230 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000231 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000232 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000233 bool ParseDirectiveFill(); // ".fill"
234 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000235 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000236 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000237 bool ParseDirectiveOrg(); // ".org"
238 // ".align{,32}", ".p2align{,w,l}"
239 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
240
241 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
242 /// accepts a single symbol (which should be a label or an external).
243 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000244
245 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
246
247 bool ParseDirectiveAbort(); // ".abort"
248 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000249 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000250
251 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000252 // ".ifb" or ".ifnb", depending on ExpectBlank.
253 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000254 // ".ifc" or ".ifnc", depending on ExpectEqual.
255 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000256 // ".ifdef" or ".ifndef", depending on expect_defined
257 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000258 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
259 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
260 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
261
262 /// ParseEscapedString - Parse the current token as a string which may include
263 /// escaped characters and return the string contents.
264 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000265
266 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
267 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000268};
269
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000270/// \brief Generic implementations of directive handling, etc. which is shared
271/// (or the default, at least) for all assembler parser.
272class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000273 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
274 void AddDirectiveHandler(StringRef Directive) {
275 getParser().AddDirectiveHandler(this, Directive,
276 HandleDirective<GenericAsmParser, Handler>);
277 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000278public:
279 GenericAsmParser() {}
280
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000281 AsmParser &getParser() {
282 return (AsmParser&) this->MCAsmParserExtension::getParser();
283 }
284
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000285 virtual void Initialize(MCAsmParser &Parser) {
286 // Call the base implementation.
287 this->MCAsmParserExtension::Initialize(Parser);
288
289 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000290 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
291 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
292 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000293 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000294
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000295 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000296 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
297 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000298 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
299 ".cfi_startproc");
300 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
301 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000302 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
303 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
305 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
307 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000308 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
309 ".cfi_def_cfa_register");
310 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
311 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000312 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
313 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000314 AddDirectiveHandler<
315 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
316 AddDirectiveHandler<
317 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000318 AddDirectiveHandler<
319 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
320 AddDirectiveHandler<
321 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000322 AddDirectiveHandler<
323 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000324 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000325 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
326 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000327 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000328 AddDirectiveHandler<
329 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000330
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000331 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000332 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
333 ".macros_on");
334 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
335 ".macros_off");
336 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
337 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
338 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000339 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000340
341 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
342 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000343 }
344
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000345 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
346
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000347 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
348 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
349 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000350 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000351 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000352 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
353 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000354 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000355 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000356 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000357 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
358 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000359 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000360 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000361 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
362 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000363 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000364 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000365 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000366 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000367
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000368 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000369 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
370 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000371 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000372
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000373 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000374};
375
376}
377
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000378namespace llvm {
379
380extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000381extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000382extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000383
384}
385
Chris Lattneraaec2052010-01-19 19:46:13 +0000386enum { DEFAULT_ADDRSPACE = 0 };
387
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000388AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000389 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000390 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000391 GenericParser(new GenericAsmParser), PlatformParser(0),
Devang Patel0db58bf2012-01-31 18:14:05 +0000392 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
393 AssemblerDialect(~0U) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000394 // Save the old handler.
395 SavedDiagHandler = SrcMgr.getDiagHandler();
396 SavedDiagContext = SrcMgr.getDiagContext();
397 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000398 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000399 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000400
401 // Initialize the generic parser.
402 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000403
404 // Initialize the platform / file format parser.
405 //
406 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
407 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000408 if (_MAI.hasMicrosoftFastStdCallMangling()) {
409 PlatformParser = createCOFFAsmParser();
410 PlatformParser->Initialize(*this);
411 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000412 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000413 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000414 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000415 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000416 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000417 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000418}
419
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000420AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000421 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
422
423 // Destroy any macros.
424 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
425 ie = MacroMap.end(); it != ie; ++it)
426 delete it->getValue();
427
Daniel Dunbare4749702010-07-12 18:12:02 +0000428 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000429 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000430}
431
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000432void AsmParser::PrintMacroInstantiations() {
433 // Print the active macro instantiation stack.
434 for (std::vector<MacroInstantiation*>::const_reverse_iterator
435 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000436 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
437 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000438}
439
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000440bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000441 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000442 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000443 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000444 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000445 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000446}
447
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000448bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000449 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000450 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000451 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000452 return true;
453}
454
Sean Callananfd0b0282010-01-21 00:19:58 +0000455bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000456 std::string IncludedFile;
457 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000458 if (NewBuf == -1)
459 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000460
Sean Callananfd0b0282010-01-21 00:19:58 +0000461 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000462
Sean Callananfd0b0282010-01-21 00:19:58 +0000463 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000464
Sean Callananfd0b0282010-01-21 00:19:58 +0000465 return false;
466}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000467
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000468/// Process the specified .incbin file by seaching for it in the include paths
469/// then just emiting the byte contents of the file to the streamer. This
470/// returns true on failure.
471bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
472 std::string IncludedFile;
473 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
474 if (NewBuf == -1)
475 return true;
476
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000477 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000478 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
479 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000480 return false;
481}
482
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000483void AsmParser::JumpToLoc(SMLoc Loc) {
484 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
485 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
486}
487
Sean Callananfd0b0282010-01-21 00:19:58 +0000488const AsmToken &AsmParser::Lex() {
489 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000490
Sean Callananfd0b0282010-01-21 00:19:58 +0000491 if (tok->is(AsmToken::Eof)) {
492 // If this is the end of an included file, pop the parent file off the
493 // include stack.
494 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
495 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000496 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000497 tok = &Lexer.Lex();
498 }
499 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000500
Sean Callananfd0b0282010-01-21 00:19:58 +0000501 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000502 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000503
Sean Callananfd0b0282010-01-21 00:19:58 +0000504 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000505}
506
Chris Lattner79180e22010-04-05 23:15:42 +0000507bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000508 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000509 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000510 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000511
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000512 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000513 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000514
515 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000516 AsmCond StartingCondState = TheCondState;
517
Kevin Enderby613b7572011-11-01 22:27:22 +0000518 // If we are generating dwarf for assembly source files save the initial text
519 // section and generate a .file directive.
520 if (getContext().getGenDwarfForAssembly()) {
521 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000522 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
523 getStreamer().EmitLabel(SectionStartSym);
524 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000525 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
526 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
527 }
528
Chris Lattnerb717fb02009-07-02 21:53:43 +0000529 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000530 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000531 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000532
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000533 // We had an error, validate that one was emitted and recover by skipping to
534 // the next line.
535 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000536 EatToEndOfStatement();
537 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000538
539 if (TheCondState.TheCond != StartingCondState.TheCond ||
540 TheCondState.Ignore != StartingCondState.Ignore)
541 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000542
543 // Check to see there are no empty DwarfFile slots.
544 const std::vector<MCDwarfFile *> &MCDwarfFiles =
545 getContext().getMCDwarfFiles();
546 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000547 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000548 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000549 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000550
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000551 // Check to see that all assembler local symbols were actually defined.
552 // Targets that don't do subsections via symbols may not want this, though,
553 // so conservatively exclude them. Only do this if we're finalizing, though,
554 // as otherwise we won't necessarilly have seen everything yet.
555 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
556 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
557 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
558 e = Symbols.end();
559 i != e; ++i) {
560 MCSymbol *Sym = i->getValue();
561 // Variable symbols may not be marked as defined, so check those
562 // explicitly. If we know it's a variable, we have a definition for
563 // the purposes of this check.
564 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
565 // FIXME: We would really like to refer back to where the symbol was
566 // first referenced for a source location. We need to add something
567 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000568 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
569 "assembler local symbol '" + Sym->getName() +
570 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000571 }
572 }
573
574
Chris Lattner79180e22010-04-05 23:15:42 +0000575 // Finalize the output stream if there are no errors and if the client wants
576 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000577 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000578 Out.Finish();
579
Chris Lattnerb717fb02009-07-02 21:53:43 +0000580 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000581}
582
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000583void AsmParser::CheckForValidSection() {
584 if (!getStreamer().getCurrentSection()) {
585 TokError("expected section directive before assembly directive");
586 Out.SwitchSection(Ctx.getMachOSection(
587 "__TEXT", "__text",
588 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
589 0, SectionKind::getText()));
590 }
591}
592
Chris Lattner2cf5f142009-06-22 01:29:09 +0000593/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
594void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000595 while (Lexer.isNot(AsmToken::EndOfStatement) &&
596 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000597 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000598
Chris Lattner2cf5f142009-06-22 01:29:09 +0000599 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000600 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000601 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000602}
603
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000604StringRef AsmParser::ParseStringToEndOfStatement() {
605 const char *Start = getTok().getLoc().getPointer();
606
607 while (Lexer.isNot(AsmToken::EndOfStatement) &&
608 Lexer.isNot(AsmToken::Eof))
609 Lex();
610
611 const char *End = getTok().getLoc().getPointer();
612 return StringRef(Start, End - Start);
613}
Chris Lattnerc4193832009-06-22 05:51:26 +0000614
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000615StringRef AsmParser::ParseStringToComma() {
616 const char *Start = getTok().getLoc().getPointer();
617
618 while (Lexer.isNot(AsmToken::EndOfStatement) &&
619 Lexer.isNot(AsmToken::Comma) &&
620 Lexer.isNot(AsmToken::Eof))
621 Lex();
622
623 const char *End = getTok().getLoc().getPointer();
624 return StringRef(Start, End - Start);
625}
626
Chris Lattner74ec1a32009-06-22 06:32:03 +0000627/// ParseParenExpr - Parse a paren expression and return it.
628/// NOTE: This assumes the leading '(' has already been consumed.
629///
630/// parenexpr ::= expr)
631///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000632bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000633 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000634 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000635 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000636 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000637 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000638 return false;
639}
Chris Lattnerc4193832009-06-22 05:51:26 +0000640
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000641/// ParseBracketExpr - Parse a bracket expression and return it.
642/// NOTE: This assumes the leading '[' has already been consumed.
643///
644/// bracketexpr ::= expr]
645///
646bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
647 if (ParseExpression(Res)) return true;
648 if (Lexer.isNot(AsmToken::RBrac))
649 return TokError("expected ']' in brackets expression");
650 EndLoc = Lexer.getLoc();
651 Lex();
652 return false;
653}
654
Chris Lattner74ec1a32009-06-22 06:32:03 +0000655/// ParsePrimaryExpr - Parse a primary expression and return it.
656/// primaryexpr ::= (parenexpr
657/// primaryexpr ::= symbol
658/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000659/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000660/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000661bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000662 switch (Lexer.getKind()) {
663 default:
664 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000665 // If we have an error assume that we've already handled it.
666 case AsmToken::Error:
667 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000668 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000669 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000670 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000671 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000672 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000673 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000674 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000675 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000676 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000677 EndLoc = Lexer.getLoc();
678
679 StringRef Identifier;
680 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000681 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000682
Daniel Dunbarfffff912009-10-16 01:34:54 +0000683 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000684 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000685 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000686
687 // Lookup the symbol variant if used.
688 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000689 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000690 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000691 if (Variant == MCSymbolRefExpr::VK_Invalid) {
692 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000693 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000694 }
695 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000696
Daniel Dunbarfffff912009-10-16 01:34:54 +0000697 // If this is an absolute variable reference, substitute it now to preserve
698 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000699 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000700 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000701 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000702
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000703 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000704 return false;
705 }
706
707 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000708 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000709 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000710 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000711 case AsmToken::Integer: {
712 SMLoc Loc = getTok().getLoc();
713 int64_t IntVal = getTok().getIntVal();
714 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000715 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000716 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000717 // Look for 'b' or 'f' following an Integer as a directional label
718 if (Lexer.getKind() == AsmToken::Identifier) {
719 StringRef IDVal = getTok().getString();
720 if (IDVal == "f" || IDVal == "b"){
721 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
722 IDVal == "f" ? 1 : 0);
723 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
724 getContext());
725 if(IDVal == "b" && Sym->isUndefined())
726 return Error(Loc, "invalid reference to undefined symbol");
727 EndLoc = Lexer.getLoc();
728 Lex(); // Eat identifier.
729 }
730 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000731 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000732 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000733 case AsmToken::Real: {
734 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000735 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000736 Res = MCConstantExpr::Create(IntVal, getContext());
737 Lex(); // Eat token.
738 return false;
739 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000740 case AsmToken::Dot: {
741 // This is a '.' reference, which references the current PC. Emit a
742 // temporary label to the streamer and refer to it.
743 MCSymbol *Sym = Ctx.CreateTempSymbol();
744 Out.EmitLabel(Sym);
745 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
746 EndLoc = Lexer.getLoc();
747 Lex(); // Eat identifier.
748 return false;
749 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000750 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000751 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000752 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000753 case AsmToken::LBrac:
754 if (!PlatformParser->HasBracketExpressions())
755 return TokError("brackets expression not supported on this target");
756 Lex(); // Eat the '['.
757 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000758 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000759 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000760 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000761 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000762 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000763 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000764 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000765 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000766 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000767 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000768 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000769 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000770 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000771 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000772 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000773 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000774 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000775 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000776 }
777}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000778
Chris Lattnerb4307b32010-01-15 19:28:38 +0000779bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000780 SMLoc EndLoc;
781 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000782}
783
Daniel Dunbarcceba832010-09-17 02:47:07 +0000784const MCExpr *
785AsmParser::ApplyModifierToExpr(const MCExpr *E,
786 MCSymbolRefExpr::VariantKind Variant) {
787 // Recurse over the given expression, rebuilding it to apply the given variant
788 // if there is exactly one symbol.
789 switch (E->getKind()) {
790 case MCExpr::Target:
791 case MCExpr::Constant:
792 return 0;
793
794 case MCExpr::SymbolRef: {
795 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
796
797 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
798 TokError("invalid variant on expression '" +
799 getTok().getIdentifier() + "' (already modified)");
800 return E;
801 }
802
803 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
804 }
805
806 case MCExpr::Unary: {
807 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
808 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
809 if (!Sub)
810 return 0;
811 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
812 }
813
814 case MCExpr::Binary: {
815 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
816 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
817 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
818
819 if (!LHS && !RHS)
820 return 0;
821
822 if (!LHS) LHS = BE->getLHS();
823 if (!RHS) RHS = BE->getRHS();
824
825 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
826 }
827 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000828
Craig Topper85814382012-02-07 05:05:23 +0000829 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000830}
831
Chris Lattner74ec1a32009-06-22 06:32:03 +0000832/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000833///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000834/// expr ::= expr &&,|| expr -> lowest.
835/// expr ::= expr |,^,&,! expr
836/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
837/// expr ::= expr <<,>> expr
838/// expr ::= expr +,- expr
839/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000840/// expr ::= primaryexpr
841///
Chris Lattner54482b42010-01-15 19:39:23 +0000842bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000843 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000844 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000845 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
846 return true;
847
Daniel Dunbarcceba832010-09-17 02:47:07 +0000848 // As a special case, we support 'a op b @ modifier' by rewriting the
849 // expression to include the modifier. This is inefficient, but in general we
850 // expect users to use 'a@modifier op b'.
851 if (Lexer.getKind() == AsmToken::At) {
852 Lex();
853
854 if (Lexer.isNot(AsmToken::Identifier))
855 return TokError("unexpected symbol modifier following '@'");
856
857 MCSymbolRefExpr::VariantKind Variant =
858 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
859 if (Variant == MCSymbolRefExpr::VK_Invalid)
860 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
861
862 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
863 if (!ModifiedRes) {
864 return TokError("invalid modifier '" + getTok().getIdentifier() +
865 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000866 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000867
Daniel Dunbarcceba832010-09-17 02:47:07 +0000868 Res = ModifiedRes;
869 Lex();
870 }
871
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000872 // Try to constant fold it up front, if possible.
873 int64_t Value;
874 if (Res->EvaluateAsAbsolute(Value))
875 Res = MCConstantExpr::Create(Value, getContext());
876
877 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000878}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000879
Chris Lattnerb4307b32010-01-15 19:28:38 +0000880bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000881 Res = 0;
882 return ParseParenExpr(Res, EndLoc) ||
883 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000884}
885
Daniel Dunbar475839e2009-06-29 20:37:27 +0000886bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000887 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000888
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000889 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000890 if (ParseExpression(Expr))
891 return true;
892
Daniel Dunbare00b0112009-10-16 01:57:52 +0000893 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000894 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000895
896 return false;
897}
898
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000899static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000900 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000901 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000902 default:
903 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000904
Jim Grosbachfbe16812011-08-20 16:24:13 +0000905 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000906 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000907 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000908 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000909 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000910 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000911 return 1;
912
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000913
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000914 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000915 //
916 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000917 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000918 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000919 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000920 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000921 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000922 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000923 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000924 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000925 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000926
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000927 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000928 case AsmToken::EqualEqual:
929 Kind = MCBinaryExpr::EQ;
930 return 3;
931 case AsmToken::ExclaimEqual:
932 case AsmToken::LessGreater:
933 Kind = MCBinaryExpr::NE;
934 return 3;
935 case AsmToken::Less:
936 Kind = MCBinaryExpr::LT;
937 return 3;
938 case AsmToken::LessEqual:
939 Kind = MCBinaryExpr::LTE;
940 return 3;
941 case AsmToken::Greater:
942 Kind = MCBinaryExpr::GT;
943 return 3;
944 case AsmToken::GreaterEqual:
945 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000946 return 3;
947
Jim Grosbachfbe16812011-08-20 16:24:13 +0000948 // Intermediate Precedence: <<, >>
949 case AsmToken::LessLess:
950 Kind = MCBinaryExpr::Shl;
951 return 4;
952 case AsmToken::GreaterGreater:
953 Kind = MCBinaryExpr::Shr;
954 return 4;
955
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000956 // High Intermediate Precedence: +, -
957 case AsmToken::Plus:
958 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000959 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000960 case AsmToken::Minus:
961 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000962 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000963
Jim Grosbachfbe16812011-08-20 16:24:13 +0000964 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000965 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000966 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000967 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000968 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000969 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000970 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000971 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000972 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000973 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000974 }
975}
976
977
978/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
979/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000980bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
981 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000982 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000983 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000984 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000985
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000986 // If the next token is lower precedence than we are allowed to eat, return
987 // successfully with what we ate already.
988 if (TokPrec < Precedence)
989 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000990
Sean Callanan79ed1a82010-01-19 20:22:31 +0000991 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000992
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000993 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000994 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000995 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000996
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000997 // If BinOp binds less tightly with RHS than the operator after RHS, let
998 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000999 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001000 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001001 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001002 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001003 }
1004
Daniel Dunbar475839e2009-06-29 20:37:27 +00001005 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001006 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001007 }
1008}
1009
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001010
1011
1012
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001013/// ParseStatement:
1014/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001015/// ::= Label* Directive ...Operands... EndOfStatement
1016/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001017bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001018 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001019 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001020 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001021 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001022 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001023
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001024 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001025 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001026 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001027 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001028 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001029 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001030 if (Lexer.is(AsmToken::Hash))
1031 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001032
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001033 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001034 if (Lexer.is(AsmToken::Integer)) {
1035 LocalLabelVal = getTok().getIntVal();
1036 if (LocalLabelVal < 0) {
1037 if (!TheCondState.Ignore)
1038 return TokError("unexpected token at start of statement");
1039 IDVal = "";
1040 }
1041 else {
1042 IDVal = getTok().getString();
1043 Lex(); // Consume the integer token to be used as an identifier token.
1044 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001045 if (!TheCondState.Ignore)
1046 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001047 }
1048 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001049
1050 } else if (Lexer.is(AsmToken::Dot)) {
1051 // Treat '.' as a valid identifier in this context.
1052 Lex();
1053 IDVal = ".";
1054
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001055 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001056 if (!TheCondState.Ignore)
1057 return TokError("unexpected token at start of statement");
1058 IDVal = "";
1059 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001060
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001061
Chris Lattner7834fac2010-04-17 18:14:27 +00001062 // Handle conditional assembly here before checking for skipping. We
1063 // have to do this so that .endif isn't skipped in a ".if 0" block for
1064 // example.
1065 if (IDVal == ".if")
1066 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001067 if (IDVal == ".ifb")
1068 return ParseDirectiveIfb(IDLoc, true);
1069 if (IDVal == ".ifnb")
1070 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00001071 if (IDVal == ".ifc")
1072 return ParseDirectiveIfc(IDLoc, true);
1073 if (IDVal == ".ifnc")
1074 return ParseDirectiveIfc(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001075 if (IDVal == ".ifdef")
1076 return ParseDirectiveIfdef(IDLoc, true);
1077 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1078 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001079 if (IDVal == ".elseif")
1080 return ParseDirectiveElseIf(IDLoc);
1081 if (IDVal == ".else")
1082 return ParseDirectiveElse(IDLoc);
1083 if (IDVal == ".endif")
1084 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001085
Chris Lattner7834fac2010-04-17 18:14:27 +00001086 // If we are in a ".if 0" block, ignore this statement.
1087 if (TheCondState.Ignore) {
1088 EatToEndOfStatement();
1089 return false;
1090 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001091
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001092 // FIXME: Recurse on local labels?
1093
1094 // See what kind of statement we have.
1095 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001096 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001097 CheckForValidSection();
1098
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001099 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001100 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001101
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001102 // Diagnose attempt to use '.' as a label.
1103 if (IDVal == ".")
1104 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1105
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001106 // Diagnose attempt to use a variable as a label.
1107 //
1108 // FIXME: Diagnostics. Note the location of the definition as a label.
1109 // FIXME: This doesn't diagnose assignment to a symbol which has been
1110 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001111 MCSymbol *Sym;
1112 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001113 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001114 else
1115 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001116 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001117 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001118
Daniel Dunbar959fd882009-08-26 22:13:22 +00001119 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001120 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001121
Kevin Enderby94c2e852011-12-09 18:09:40 +00001122 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001123 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001124 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001125 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1126 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001127
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001128 // Consume any end of statement token, if present, to avoid spurious
1129 // AddBlankLine calls().
1130 if (Lexer.is(AsmToken::EndOfStatement)) {
1131 Lex();
1132 if (Lexer.is(AsmToken::Eof))
1133 return false;
1134 }
1135
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001136 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001137 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001138
Daniel Dunbar3f872332009-07-28 16:08:33 +00001139 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001140 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001141 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001142
Nico Weber4c4c7322011-01-28 03:04:41 +00001143 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001144
1145 default: // Normal instruction or directive.
1146 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001147 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001148
1149 // If macros are enabled, check to see if this is a macro instantiation.
1150 if (MacrosEnabled)
1151 if (const Macro *M = MacroMap.lookup(IDVal))
1152 return HandleMacroEntry(IDVal, IDLoc, M);
1153
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001154 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001155 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001156 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001157 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001158 return ParseDirectiveSet(IDVal, true);
1159 if (IDVal == ".equiv")
1160 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001161
Daniel Dunbara0d14262009-06-24 23:30:00 +00001162 // Data directives
1163
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001164 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001165 return ParseDirectiveAscii(IDVal, false);
1166 if (IDVal == ".asciz" || IDVal == ".string")
1167 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001168
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001169 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001170 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001171 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001172 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001173 if (IDVal == ".value")
1174 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001175 if (IDVal == ".2byte")
1176 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001177 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001178 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001179 if (IDVal == ".int")
1180 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001181 if (IDVal == ".4byte")
1182 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001183 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001184 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001185 if (IDVal == ".8byte")
1186 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001187 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001188 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1189 if (IDVal == ".double")
1190 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001191
Eli Friedman5d68ec22010-07-19 04:17:25 +00001192 if (IDVal == ".align") {
1193 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1194 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1195 }
1196 if (IDVal == ".align32") {
1197 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1198 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1199 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001200 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001201 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001202 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001203 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001204 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001205 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001206 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001207 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001208 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001209 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001210 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001211 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1212
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001213 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001214 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001215
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001216 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001217 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001218 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001219 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001220 if (IDVal == ".zero")
1221 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001222
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001223 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001224
Benjamin Kramere14a3c52012-05-12 11:18:59 +00001225 if (IDVal == ".extern") {
1226 EatToEndOfStatement(); // .extern is the default, ignore it.
1227 return false;
1228 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001229 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001230 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001231 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001232 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001233 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001234 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001235 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001236 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001237 if (IDVal == ".symbol_resolver")
1238 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001239 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001240 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001241 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001242 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001243 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001244 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001245 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001246 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001247 if (IDVal == ".weak_def_can_be_hidden")
1248 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001249
Hans Wennborg5cc64912011-06-18 13:51:54 +00001250 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001251 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001252 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001253 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001254
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001255 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001256 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001257 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001258 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001259 if (IDVal == ".incbin")
1260 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001261
Benjamin Kramer5cdf0ad2012-05-12 11:19:04 +00001262 if (IDVal == ".code16" || IDVal == ".code16gcc")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001263 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001264
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001265 // Look up the handler in the handler table.
1266 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1267 DirectiveMap.lookup(IDVal);
1268 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001269 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001270
Kevin Enderby9c656452009-09-10 20:51:44 +00001271 // Target hook for parsing target specific directives.
1272 if (!getTargetParser().ParseDirective(ID))
1273 return false;
1274
Jim Grosbach686c0182012-05-01 18:38:27 +00001275 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001276 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001277
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001278 CheckForValidSection();
1279
Chris Lattnera7f13542010-05-19 23:34:33 +00001280 // Canonicalize the opcode to lower case.
1281 SmallString<128> Opcode;
1282 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1283 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001284
Chris Lattner98986712010-01-14 22:21:20 +00001285 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001286 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001287 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001288
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001289 // Dump the parsed representation, if requested.
1290 if (getShowParsedOperands()) {
1291 SmallString<256> Str;
1292 raw_svector_ostream OS(Str);
1293 OS << "parsed instruction: [";
1294 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1295 if (i != 0)
1296 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001297 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001298 }
1299 OS << "]";
1300
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001301 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001302 }
1303
Kevin Enderby613b7572011-11-01 22:27:22 +00001304 // If we are generating dwarf for assembly source files and the current
1305 // section is the initial text section then generate a .loc directive for
1306 // the instruction.
1307 if (!HadError && getContext().getGenDwarfForAssembly() &&
1308 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1309 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1310 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1311 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001312 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001313 StringRef());
1314 }
1315
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001316 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001317 if (!HadError)
1318 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1319 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001320
Chris Lattner98986712010-01-14 22:21:20 +00001321 // Free any parsed operands.
1322 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1323 delete ParsedOperands[i];
1324
Chris Lattnercbf8a982010-09-11 16:18:25 +00001325 // Don't skip the rest of the line, the instruction parser is responsible for
1326 // that.
1327 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001328}
Chris Lattner9a023f72009-06-24 04:43:34 +00001329
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001330/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1331/// since they may not be able to be tokenized to get to the end of line token.
1332void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001333 if (!Lexer.is(AsmToken::EndOfStatement))
1334 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001335 // Eat EOL.
1336 Lex();
1337}
1338
1339/// ParseCppHashLineFilenameComment as this:
1340/// ::= # number "filename"
1341/// or just as a full line comment if it doesn't have a number and a string.
1342bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1343 Lex(); // Eat the hash token.
1344
1345 if (getLexer().isNot(AsmToken::Integer)) {
1346 // Consume the line since in cases it is not a well-formed line directive,
1347 // as if were simply a full line comment.
1348 EatToEndOfLine();
1349 return false;
1350 }
1351
1352 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001353 Lex();
1354
1355 if (getLexer().isNot(AsmToken::String)) {
1356 EatToEndOfLine();
1357 return false;
1358 }
1359
1360 StringRef Filename = getTok().getString();
1361 // Get rid of the enclosing quotes.
1362 Filename = Filename.substr(1, Filename.size()-2);
1363
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001364 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1365 CppHashLoc = L;
1366 CppHashFilename = Filename;
1367 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001368
1369 // Ignore any trailing characters, they're just comment.
1370 EatToEndOfLine();
1371 return false;
1372}
1373
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001374/// DiagHandler - will use the the last parsed cpp hash line filename comment
1375/// for the Filename and LineNo if any in the diagnostic.
1376void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1377 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1378 raw_ostream &OS = errs();
1379
1380 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1381 const SMLoc &DiagLoc = Diag.getLoc();
1382 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1383 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1384
1385 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1386 // before printing the message.
1387 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001388 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001389 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1390 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1391 }
1392
1393 // If we have not parsed a cpp hash line filename comment or the source
1394 // manager changed or buffer changed (like in a nested include) then just
1395 // print the normal diagnostic using its Filename and LineNo.
1396 if (!Parser->CppHashLineNumber ||
1397 &DiagSrcMgr != &Parser->SrcMgr ||
1398 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001399 if (Parser->SavedDiagHandler)
1400 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1401 else
1402 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001403 return;
1404 }
1405
1406 // Use the CppHashFilename and calculate a line number based on the
1407 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1408 // the diagnostic.
1409 const std::string Filename = Parser->CppHashFilename;
1410
1411 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1412 int CppHashLocLineNo =
1413 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1414 int LineNo = Parser->CppHashLineNumber - 1 +
1415 (DiagLocLineNo - CppHashLocLineNo);
1416
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001417 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1418 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001419 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001420 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001421
Benjamin Kramer04a04262011-10-16 10:48:29 +00001422 if (Parser->SavedDiagHandler)
1423 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1424 else
1425 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001426}
1427
Rafael Espindola65366442011-06-05 02:43:45 +00001428bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1429 const std::vector<StringRef> &Parameters,
1430 const std::vector<std::vector<AsmToken> > &A,
1431 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001432 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001433 unsigned NParameters = Parameters.size();
1434 if (NParameters != 0 && NParameters != A.size())
1435 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001436
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001437 while (!Body.empty()) {
1438 // Scan for the next substitution.
1439 std::size_t End = Body.size(), Pos = 0;
1440 for (; Pos != End; ++Pos) {
1441 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001442 if (!NParameters) {
1443 // This macro has no parameters, look for $0, $1, etc.
1444 if (Body[Pos] != '$' || Pos + 1 == End)
1445 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001446
Rafael Espindola65366442011-06-05 02:43:45 +00001447 char Next = Body[Pos + 1];
1448 if (Next == '$' || Next == 'n' || isdigit(Next))
1449 break;
1450 } else {
1451 // This macro has parameters, look for \foo, \bar, etc.
1452 if (Body[Pos] == '\\' && Pos + 1 != End)
1453 break;
1454 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001455 }
1456
1457 // Add the prefix.
1458 OS << Body.slice(0, Pos);
1459
1460 // Check if we reached the end.
1461 if (Pos == End)
1462 break;
1463
Rafael Espindola65366442011-06-05 02:43:45 +00001464 if (!NParameters) {
1465 switch (Body[Pos+1]) {
1466 // $$ => $
1467 case '$':
1468 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001469 break;
1470
Rafael Espindola65366442011-06-05 02:43:45 +00001471 // $n => number of arguments
1472 case 'n':
1473 OS << A.size();
1474 break;
1475
1476 // $[0-9] => argument
1477 default: {
1478 // Missing arguments are ignored.
1479 unsigned Index = Body[Pos+1] - '0';
1480 if (Index >= A.size())
1481 break;
1482
1483 // Otherwise substitute with the token values, with spaces eliminated.
1484 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1485 ie = A[Index].end(); it != ie; ++it)
1486 OS << it->getString();
1487 break;
1488 }
1489 }
1490 Pos += 2;
1491 } else {
1492 unsigned I = Pos + 1;
1493 while (isalnum(Body[I]) && I + 1 != End)
1494 ++I;
1495
1496 const char *Begin = Body.data() + Pos +1;
1497 StringRef Argument(Begin, I - (Pos +1));
1498 unsigned Index = 0;
1499 for (; Index < NParameters; ++Index)
1500 if (Parameters[Index] == Argument)
1501 break;
1502
1503 // FIXME: We should error at the macro definition.
1504 if (Index == NParameters)
1505 return Error(L, "Parameter not found");
1506
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001507 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1508 ie = A[Index].end(); it != ie; ++it)
1509 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001510
Rafael Espindola65366442011-06-05 02:43:45 +00001511 Pos += 1 + Argument.size();
1512 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001513 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001514 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001515 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001516
1517 // We include the .endmacro in the buffer as our queue to exit the macro
1518 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001519 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001520 return false;
1521}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001522
Rafael Espindola65366442011-06-05 02:43:45 +00001523MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1524 MemoryBuffer *I)
1525 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1526{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001527}
1528
1529bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1530 const Macro *M) {
1531 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1532 // this, although we should protect against infinite loops.
1533 if (ActiveMacros.size() == 20)
1534 return TokError("macros cannot be nested more than 20 levels deep");
1535
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001536 // Parse the macro instantiation arguments.
1537 std::vector<std::vector<AsmToken> > MacroArguments;
1538 MacroArguments.push_back(std::vector<AsmToken>());
1539 unsigned ParenLevel = 0;
1540 for (;;) {
1541 if (Lexer.is(AsmToken::Eof))
1542 return TokError("unexpected token in macro instantiation");
1543 if (Lexer.is(AsmToken::EndOfStatement))
1544 break;
1545
1546 // If we aren't inside parentheses and this is a comma, start a new token
1547 // list.
1548 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1549 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001550 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001551 // Adjust the current parentheses level.
1552 if (Lexer.is(AsmToken::LParen))
1553 ++ParenLevel;
1554 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1555 --ParenLevel;
1556
1557 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001558 MacroArguments.back().push_back(getTok());
1559 }
1560 Lex();
1561 }
Jim Grosbach68f89a62012-04-16 21:18:49 +00001562 // If the last argument didn't end up with any tokens, it's not a real
1563 // argument and we should remove it from the list. This happens with either
1564 // a tailing comma or an empty argument list.
1565 if (MacroArguments.back().empty())
1566 MacroArguments.pop_back();
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001567
Rafael Espindola65366442011-06-05 02:43:45 +00001568 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1569 // to hold the macro body with substitutions.
1570 SmallString<256> Buf;
1571 StringRef Body = M->Body;
1572
1573 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1574 return true;
1575
1576 MemoryBuffer *Instantiation =
1577 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1578
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001579 // Create the macro instantiation object and add to the current macro
1580 // instantiation stack.
1581 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001582 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001583 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001584 ActiveMacros.push_back(MI);
1585
1586 // Jump to the macro instantiation and prime the lexer.
1587 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1588 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1589 Lex();
1590
1591 return false;
1592}
1593
1594void AsmParser::HandleMacroExit() {
1595 // Jump to the EndOfStatement we should return to, and consume it.
1596 JumpToLoc(ActiveMacros.back()->ExitLoc);
1597 Lex();
1598
1599 // Pop the instantiation entry.
1600 delete ActiveMacros.back();
1601 ActiveMacros.pop_back();
1602}
1603
Rafael Espindolae71cc862012-01-28 05:57:00 +00001604static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001605 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001606 case MCExpr::Binary: {
1607 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1608 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001609 break;
1610 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001611 case MCExpr::Target:
1612 case MCExpr::Constant:
1613 return false;
1614 case MCExpr::SymbolRef: {
1615 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001616 if (S.isVariable())
1617 return IsUsedIn(Sym, S.getVariableValue());
1618 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001619 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001620 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001621 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001622 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001623
1624 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001625}
1626
Nico Weber4c4c7322011-01-28 03:04:41 +00001627bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001628 // FIXME: Use better location, we should use proper tokens.
1629 SMLoc EqualLoc = Lexer.getLoc();
1630
Daniel Dunbar821e3332009-08-31 08:09:28 +00001631 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001632 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001633 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001634
Rafael Espindolae71cc862012-01-28 05:57:00 +00001635 // Note: we don't count b as used in "a = b". This is to allow
1636 // a = b
1637 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001638
Daniel Dunbar3f872332009-07-28 16:08:33 +00001639 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001640 return TokError("unexpected token in assignment");
1641
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001642 // Error on assignment to '.'.
1643 if (Name == ".") {
1644 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1645 "(use '.space' or '.org').)"));
1646 }
1647
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001648 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001649 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001650
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001651 // Validate that the LHS is allowed to be a variable (either it has not been
1652 // used as a symbol, or it is an absolute symbol).
1653 MCSymbol *Sym = getContext().LookupSymbol(Name);
1654 if (Sym) {
1655 // Diagnose assignment to a label.
1656 //
1657 // FIXME: Diagnostics. Note the location of the definition as a label.
1658 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001659 if (IsUsedIn(Sym, Value))
1660 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1661 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001662 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001663 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1664 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001665 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001666 return Error(EqualLoc, "redefinition of '" + Name + "'");
1667 else if (!Sym->isVariable())
1668 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001669 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001670 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1671 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001672
1673 // Don't count these checks as uses.
1674 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001675 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001676 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001677
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001678 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001679
1680 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001681 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001682
1683 return false;
1684}
1685
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001686/// ParseIdentifier:
1687/// ::= identifier
1688/// ::= string
1689bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001690 // The assembler has relaxed rules for accepting identifiers, in particular we
1691 // allow things like '.globl $foo', which would normally be separate
1692 // tokens. At this level, we have already lexed so we cannot (currently)
1693 // handle this as a context dependent token, instead we detect adjacent tokens
1694 // and return the combined identifier.
1695 if (Lexer.is(AsmToken::Dollar)) {
1696 SMLoc DollarLoc = getLexer().getLoc();
1697
1698 // Consume the dollar sign, and check for a following identifier.
1699 Lex();
1700 if (Lexer.isNot(AsmToken::Identifier))
1701 return true;
1702
1703 // We have a '$' followed by an identifier, make sure they are adjacent.
1704 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1705 return true;
1706
1707 // Construct the joined identifier and consume the token.
1708 Res = StringRef(DollarLoc.getPointer(),
1709 getTok().getIdentifier().size() + 1);
1710 Lex();
1711 return false;
1712 }
1713
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001714 if (Lexer.isNot(AsmToken::Identifier) &&
1715 Lexer.isNot(AsmToken::String))
1716 return true;
1717
Sean Callanan18b83232010-01-19 21:44:56 +00001718 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001719
Sean Callanan79ed1a82010-01-19 20:22:31 +00001720 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001721
1722 return false;
1723}
1724
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001725/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001726/// ::= .equ identifier ',' expression
1727/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001728/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001729bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001730 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001731
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001732 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001733 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001734
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001735 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001736 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001737 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001738
Nico Weber4c4c7322011-01-28 03:04:41 +00001739 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001740}
1741
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001742bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001743 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001744
1745 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001746 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001747 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1748 if (Str[i] != '\\') {
1749 Data += Str[i];
1750 continue;
1751 }
1752
1753 // Recognize escaped characters. Note that this escape semantics currently
1754 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1755 ++i;
1756 if (i == e)
1757 return TokError("unexpected backslash at end of string");
1758
1759 // Recognize octal sequences.
1760 if ((unsigned) (Str[i] - '0') <= 7) {
1761 // Consume up to three octal characters.
1762 unsigned Value = Str[i] - '0';
1763
1764 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1765 ++i;
1766 Value = Value * 8 + (Str[i] - '0');
1767
1768 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1769 ++i;
1770 Value = Value * 8 + (Str[i] - '0');
1771 }
1772 }
1773
1774 if (Value > 255)
1775 return TokError("invalid octal escape sequence (out of range)");
1776
1777 Data += (unsigned char) Value;
1778 continue;
1779 }
1780
1781 // Otherwise recognize individual escapes.
1782 switch (Str[i]) {
1783 default:
1784 // Just reject invalid escape sequences for now.
1785 return TokError("invalid escape sequence (unrecognized character)");
1786
1787 case 'b': Data += '\b'; break;
1788 case 'f': Data += '\f'; break;
1789 case 'n': Data += '\n'; break;
1790 case 'r': Data += '\r'; break;
1791 case 't': Data += '\t'; break;
1792 case '"': Data += '"'; break;
1793 case '\\': Data += '\\'; break;
1794 }
1795 }
1796
1797 return false;
1798}
1799
Daniel Dunbara0d14262009-06-24 23:30:00 +00001800/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001801/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1802bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001803 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001804 CheckForValidSection();
1805
Daniel Dunbara0d14262009-06-24 23:30:00 +00001806 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001807 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001808 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001809
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001810 std::string Data;
1811 if (ParseEscapedString(Data))
1812 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001813
1814 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001815 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001816 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1817
Sean Callanan79ed1a82010-01-19 20:22:31 +00001818 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001819
1820 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001821 break;
1822
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001823 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001824 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001825 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001826 }
1827 }
1828
Sean Callanan79ed1a82010-01-19 20:22:31 +00001829 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001830 return false;
1831}
1832
1833/// ParseDirectiveValue
1834/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1835bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001836 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001837 CheckForValidSection();
1838
Daniel Dunbara0d14262009-06-24 23:30:00 +00001839 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001840 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001841 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001842 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001843 return true;
1844
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001845 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001846 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1847 assert(Size <= 8 && "Invalid size");
1848 uint64_t IntValue = MCE->getValue();
1849 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1850 return Error(ExprLoc, "literal value out of range for directive");
1851 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1852 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001853 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001854
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001855 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001856 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001857
Daniel Dunbara0d14262009-06-24 23:30:00 +00001858 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001859 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001860 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001861 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001862 }
1863 }
1864
Sean Callanan79ed1a82010-01-19 20:22:31 +00001865 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001866 return false;
1867}
1868
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001869/// ParseDirectiveRealValue
1870/// ::= (.single | .double) [ expression (, expression)* ]
1871bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1872 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1873 CheckForValidSection();
1874
1875 for (;;) {
1876 // We don't truly support arithmetic on floating point expressions, so we
1877 // have to manually parse unary prefixes.
1878 bool IsNeg = false;
1879 if (getLexer().is(AsmToken::Minus)) {
1880 Lex();
1881 IsNeg = true;
1882 } else if (getLexer().is(AsmToken::Plus))
1883 Lex();
1884
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001885 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001886 getLexer().isNot(AsmToken::Real) &&
1887 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001888 return TokError("unexpected token in directive");
1889
1890 // Convert to an APFloat.
1891 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001892 StringRef IDVal = getTok().getString();
1893 if (getLexer().is(AsmToken::Identifier)) {
1894 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1895 Value = APFloat::getInf(Semantics);
1896 else if (!IDVal.compare_lower("nan"))
1897 Value = APFloat::getNaN(Semantics, false, ~0);
1898 else
1899 return TokError("invalid floating point literal");
1900 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001901 APFloat::opInvalidOp)
1902 return TokError("invalid floating point literal");
1903 if (IsNeg)
1904 Value.changeSign();
1905
1906 // Consume the numeric token.
1907 Lex();
1908
1909 // Emit the value as an integer.
1910 APInt AsInt = Value.bitcastToAPInt();
1911 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1912 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1913
1914 if (getLexer().is(AsmToken::EndOfStatement))
1915 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001916
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001917 if (getLexer().isNot(AsmToken::Comma))
1918 return TokError("unexpected token in directive");
1919 Lex();
1920 }
1921 }
1922
1923 Lex();
1924 return false;
1925}
1926
Daniel Dunbara0d14262009-06-24 23:30:00 +00001927/// ParseDirectiveSpace
1928/// ::= .space expression [ , expression ]
1929bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001930 CheckForValidSection();
1931
Daniel Dunbara0d14262009-06-24 23:30:00 +00001932 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001933 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001934 return true;
1935
1936 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001937 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1938 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001939 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001940 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001941
Daniel Dunbar475839e2009-06-29 20:37:27 +00001942 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001943 return true;
1944
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001945 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001946 return TokError("unexpected token in '.space' directive");
1947 }
1948
Sean Callanan79ed1a82010-01-19 20:22:31 +00001949 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001950
1951 if (NumBytes <= 0)
1952 return TokError("invalid number of bytes in '.space' directive");
1953
1954 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001955 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001956
1957 return false;
1958}
1959
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001960/// ParseDirectiveZero
1961/// ::= .zero expression
1962bool AsmParser::ParseDirectiveZero() {
1963 CheckForValidSection();
1964
1965 int64_t NumBytes;
1966 if (ParseAbsoluteExpression(NumBytes))
1967 return true;
1968
Rafael Espindolae452b172010-10-05 19:42:57 +00001969 int64_t Val = 0;
1970 if (getLexer().is(AsmToken::Comma)) {
1971 Lex();
1972 if (ParseAbsoluteExpression(Val))
1973 return true;
1974 }
1975
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001976 if (getLexer().isNot(AsmToken::EndOfStatement))
1977 return TokError("unexpected token in '.zero' directive");
1978
1979 Lex();
1980
Rafael Espindolae452b172010-10-05 19:42:57 +00001981 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001982
1983 return false;
1984}
1985
Daniel Dunbara0d14262009-06-24 23:30:00 +00001986/// ParseDirectiveFill
1987/// ::= .fill expression , expression , expression
1988bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001989 CheckForValidSection();
1990
Daniel Dunbara0d14262009-06-24 23:30:00 +00001991 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001992 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001993 return true;
1994
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001995 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001996 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001997 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001998
Daniel Dunbara0d14262009-06-24 23:30:00 +00001999 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002000 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002001 return true;
2002
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002003 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002004 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002005 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002006
Daniel Dunbara0d14262009-06-24 23:30:00 +00002007 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002008 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002009 return true;
2010
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002011 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002012 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002013
Sean Callanan79ed1a82010-01-19 20:22:31 +00002014 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002015
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002016 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2017 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002018
2019 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002020 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002021
2022 return false;
2023}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002024
2025/// ParseDirectiveOrg
2026/// ::= .org expression [ , expression ]
2027bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002028 CheckForValidSection();
2029
Daniel Dunbar821e3332009-08-31 08:09:28 +00002030 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002031 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002032 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002033 return true;
2034
2035 // Parse optional fill expression.
2036 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002037 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2038 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002039 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002040 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002041
Daniel Dunbar475839e2009-06-29 20:37:27 +00002042 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002043 return true;
2044
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002045 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002046 return TokError("unexpected token in '.org' directive");
2047 }
2048
Sean Callanan79ed1a82010-01-19 20:22:31 +00002049 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002050
Jim Grosbachebd4c052012-01-27 00:37:08 +00002051 // Only limited forms of relocatable expressions are accepted here, it
2052 // has to be relative to the current section. The streamer will return
2053 // 'true' if the expression wasn't evaluatable.
2054 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2055 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002056
2057 return false;
2058}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002059
2060/// ParseDirectiveAlign
2061/// ::= {.align, ...} expression [ , expression [ , expression ]]
2062bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002063 CheckForValidSection();
2064
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002065 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002066 int64_t Alignment;
2067 if (ParseAbsoluteExpression(Alignment))
2068 return true;
2069
2070 SMLoc MaxBytesLoc;
2071 bool HasFillExpr = false;
2072 int64_t FillExpr = 0;
2073 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002074 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2075 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002076 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002077 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002078
2079 // The fill expression can be omitted while specifying a maximum number of
2080 // alignment bytes, e.g:
2081 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002082 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002083 HasFillExpr = true;
2084 if (ParseAbsoluteExpression(FillExpr))
2085 return true;
2086 }
2087
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002088 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2089 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002090 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002091 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002092
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002093 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002094 if (ParseAbsoluteExpression(MaxBytesToFill))
2095 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002096
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002097 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002098 return TokError("unexpected token in directive");
2099 }
2100 }
2101
Sean Callanan79ed1a82010-01-19 20:22:31 +00002102 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002103
Daniel Dunbar648ac512010-05-17 21:54:30 +00002104 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002105 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002106
2107 // Compute alignment in bytes.
2108 if (IsPow2) {
2109 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002110 if (Alignment >= 32) {
2111 Error(AlignmentLoc, "invalid alignment value");
2112 Alignment = 31;
2113 }
2114
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002115 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002116 }
2117
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002118 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002119 if (MaxBytesLoc.isValid()) {
2120 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002121 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2122 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002123 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002124 }
2125
2126 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002127 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2128 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002129 MaxBytesToFill = 0;
2130 }
2131 }
2132
Daniel Dunbar648ac512010-05-17 21:54:30 +00002133 // Check whether we should use optimal code alignment for this .align
2134 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002135 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002136 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2137 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002138 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002139 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002140 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002141 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2142 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002143 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002144
2145 return false;
2146}
2147
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002148/// ParseDirectiveSymbolAttribute
2149/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002150bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002151 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002152 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002153 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002154 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002155
2156 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002157 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002158
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002159 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002160
Jim Grosbach10ec6502011-09-15 17:56:49 +00002161 // Assembler local symbols don't make any sense here. Complain loudly.
2162 if (Sym->isTemporary())
2163 return Error(Loc, "non-local symbol required in directive");
2164
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002165 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002166
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002167 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002168 break;
2169
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002170 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002171 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002172 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002173 }
2174 }
2175
Sean Callanan79ed1a82010-01-19 20:22:31 +00002176 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002177 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002178}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002179
2180/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002181/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2182bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002183 CheckForValidSection();
2184
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002185 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002186 StringRef Name;
2187 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002188 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002189
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002190 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002191 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002192
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002193 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002194 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002195 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002196
2197 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002198 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002199 if (ParseAbsoluteExpression(Size))
2200 return true;
2201
2202 int64_t Pow2Alignment = 0;
2203 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002204 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002205 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002206 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002207 if (ParseAbsoluteExpression(Pow2Alignment))
2208 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002209
Chris Lattner258281d2010-01-19 06:22:22 +00002210 // If this target takes alignments in bytes (not log) validate and convert.
2211 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2212 if (!isPowerOf2_64(Pow2Alignment))
2213 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2214 Pow2Alignment = Log2_64(Pow2Alignment);
2215 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002216 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002217
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002218 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002219 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002220
Sean Callanan79ed1a82010-01-19 20:22:31 +00002221 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002222
Chris Lattner1fc3d752009-07-09 17:25:12 +00002223 // NOTE: a size of zero for a .comm should create a undefined symbol
2224 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002225 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002226 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2227 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002228
Eric Christopherc260a3e2010-05-14 01:38:54 +00002229 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002230 // may internally end up wanting an alignment in bytes.
2231 // FIXME: Diagnose overflow.
2232 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002233 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2234 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002235
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002236 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002237 return Error(IDLoc, "invalid symbol redefinition");
2238
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002239 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002240 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002241 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002242 getStreamer().EmitZerofill(Ctx.getMachOSection(
2243 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2244 0, SectionKind::getBSS()),
2245 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002246 return false;
2247 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002248
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002249 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002250 return false;
2251}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002252
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002253/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002254/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002255bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002256 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002257 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002258
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002259 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002260 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002261 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002262
Sean Callanan79ed1a82010-01-19 20:22:31 +00002263 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002264
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002265 if (Str.empty())
2266 Error(Loc, ".abort detected. Assembly stopping.");
2267 else
2268 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002269 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002270
2271 return false;
2272}
Kevin Enderby71148242009-07-14 21:35:03 +00002273
Kevin Enderby1f049b22009-07-14 23:21:55 +00002274/// ParseDirectiveInclude
2275/// ::= .include "filename"
2276bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002277 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002278 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002279
Sean Callanan18b83232010-01-19 21:44:56 +00002280 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002281 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002282 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002283
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002284 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002285 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002286
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002287 // Strip the quotes.
2288 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002289
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002290 // Attempt to switch the lexer to the included file before consuming the end
2291 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002292 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002293 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002294 return true;
2295 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002296
2297 return false;
2298}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002299
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002300/// ParseDirectiveIncbin
2301/// ::= .incbin "filename"
2302bool AsmParser::ParseDirectiveIncbin() {
2303 if (getLexer().isNot(AsmToken::String))
2304 return TokError("expected string in '.incbin' directive");
2305
2306 std::string Filename = getTok().getString();
2307 SMLoc IncbinLoc = getLexer().getLoc();
2308 Lex();
2309
2310 if (getLexer().isNot(AsmToken::EndOfStatement))
2311 return TokError("unexpected token in '.incbin' directive");
2312
2313 // Strip the quotes.
2314 Filename = Filename.substr(1, Filename.size()-2);
2315
2316 // Attempt to process the included file.
2317 if (ProcessIncbinFile(Filename)) {
2318 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2319 return true;
2320 }
2321
2322 return false;
2323}
2324
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002325/// ParseDirectiveIf
2326/// ::= .if expression
2327bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002328 TheCondStack.push_back(TheCondState);
2329 TheCondState.TheCond = AsmCond::IfCond;
2330 if(TheCondState.Ignore) {
2331 EatToEndOfStatement();
2332 }
2333 else {
2334 int64_t ExprValue;
2335 if (ParseAbsoluteExpression(ExprValue))
2336 return true;
2337
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002338 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002339 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002340
Sean Callanan79ed1a82010-01-19 20:22:31 +00002341 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002342
2343 TheCondState.CondMet = ExprValue;
2344 TheCondState.Ignore = !TheCondState.CondMet;
2345 }
2346
2347 return false;
2348}
2349
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002350/// ParseDirectiveIfb
2351/// ::= .ifb string
2352bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2353 TheCondStack.push_back(TheCondState);
2354 TheCondState.TheCond = AsmCond::IfCond;
2355
2356 if(TheCondState.Ignore) {
2357 EatToEndOfStatement();
2358 } else {
2359 StringRef Str = ParseStringToEndOfStatement();
2360
2361 if (getLexer().isNot(AsmToken::EndOfStatement))
2362 return TokError("unexpected token in '.ifb' directive");
2363
2364 Lex();
2365
2366 TheCondState.CondMet = ExpectBlank == Str.empty();
2367 TheCondState.Ignore = !TheCondState.CondMet;
2368 }
2369
2370 return false;
2371}
2372
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002373/// ParseDirectiveIfc
2374/// ::= .ifc string1, string2
2375bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2376 TheCondStack.push_back(TheCondState);
2377 TheCondState.TheCond = AsmCond::IfCond;
2378
2379 if(TheCondState.Ignore) {
2380 EatToEndOfStatement();
2381 } else {
2382 StringRef Str1 = ParseStringToComma();
2383
2384 if (getLexer().isNot(AsmToken::Comma))
2385 return TokError("unexpected token in '.ifc' directive");
2386
2387 Lex();
2388
2389 StringRef Str2 = ParseStringToEndOfStatement();
2390
2391 if (getLexer().isNot(AsmToken::EndOfStatement))
2392 return TokError("unexpected token in '.ifc' directive");
2393
2394 Lex();
2395
2396 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2397 TheCondState.Ignore = !TheCondState.CondMet;
2398 }
2399
2400 return false;
2401}
2402
2403/// ParseDirectiveIfdef
2404/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002405bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2406 StringRef Name;
2407 TheCondStack.push_back(TheCondState);
2408 TheCondState.TheCond = AsmCond::IfCond;
2409
2410 if (TheCondState.Ignore) {
2411 EatToEndOfStatement();
2412 } else {
2413 if (ParseIdentifier(Name))
2414 return TokError("expected identifier after '.ifdef'");
2415
2416 Lex();
2417
2418 MCSymbol *Sym = getContext().LookupSymbol(Name);
2419
2420 if (expect_defined)
2421 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2422 else
2423 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2424 TheCondState.Ignore = !TheCondState.CondMet;
2425 }
2426
2427 return false;
2428}
2429
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002430/// ParseDirectiveElseIf
2431/// ::= .elseif expression
2432bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2433 if (TheCondState.TheCond != AsmCond::IfCond &&
2434 TheCondState.TheCond != AsmCond::ElseIfCond)
2435 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2436 " an .elseif");
2437 TheCondState.TheCond = AsmCond::ElseIfCond;
2438
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002439 bool LastIgnoreState = false;
2440 if (!TheCondStack.empty())
2441 LastIgnoreState = TheCondStack.back().Ignore;
2442 if (LastIgnoreState || TheCondState.CondMet) {
2443 TheCondState.Ignore = true;
2444 EatToEndOfStatement();
2445 }
2446 else {
2447 int64_t ExprValue;
2448 if (ParseAbsoluteExpression(ExprValue))
2449 return true;
2450
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002451 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002452 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002453
Sean Callanan79ed1a82010-01-19 20:22:31 +00002454 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002455 TheCondState.CondMet = ExprValue;
2456 TheCondState.Ignore = !TheCondState.CondMet;
2457 }
2458
2459 return false;
2460}
2461
2462/// ParseDirectiveElse
2463/// ::= .else
2464bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002465 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002466 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002467
Sean Callanan79ed1a82010-01-19 20:22:31 +00002468 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002469
2470 if (TheCondState.TheCond != AsmCond::IfCond &&
2471 TheCondState.TheCond != AsmCond::ElseIfCond)
2472 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2473 ".elseif");
2474 TheCondState.TheCond = AsmCond::ElseCond;
2475 bool LastIgnoreState = false;
2476 if (!TheCondStack.empty())
2477 LastIgnoreState = TheCondStack.back().Ignore;
2478 if (LastIgnoreState || TheCondState.CondMet)
2479 TheCondState.Ignore = true;
2480 else
2481 TheCondState.Ignore = false;
2482
2483 return false;
2484}
2485
2486/// ParseDirectiveEndIf
2487/// ::= .endif
2488bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002489 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002490 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002491
Sean Callanan79ed1a82010-01-19 20:22:31 +00002492 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002493
2494 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2495 TheCondStack.empty())
2496 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2497 ".else");
2498 if (!TheCondStack.empty()) {
2499 TheCondState = TheCondStack.back();
2500 TheCondStack.pop_back();
2501 }
2502
2503 return false;
2504}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002505
2506/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002507/// ::= .file [number] filename
2508/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002509bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002510 // FIXME: I'm not sure what this is.
2511 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002512 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002513 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002514 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002515 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002516
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002517 if (FileNumber < 1)
2518 return TokError("file number less than one");
2519 }
2520
Daniel Dunbareceec052010-07-12 17:45:27 +00002521 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002522 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002523
Nick Lewycky44d798d2011-10-17 23:05:28 +00002524 // Usually the directory and filename together, otherwise just the directory.
2525 StringRef Path = getTok().getString();
2526 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002527 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002528
Nick Lewycky44d798d2011-10-17 23:05:28 +00002529 StringRef Directory;
2530 StringRef Filename;
2531 if (getLexer().is(AsmToken::String)) {
2532 if (FileNumber == -1)
2533 return TokError("explicit path specified, but no file number");
2534 Filename = getTok().getString();
2535 Filename = Filename.substr(1, Filename.size()-2);
2536 Directory = Path;
2537 Lex();
2538 } else {
2539 Filename = Path;
2540 }
2541
Daniel Dunbareceec052010-07-12 17:45:27 +00002542 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002543 return TokError("unexpected token in '.file' directive");
2544
Chris Lattnerd32e8032010-01-25 19:02:58 +00002545 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002546 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002547 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002548 if (getContext().getGenDwarfForAssembly() == true)
2549 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2550 "used to generate dwarf debug info for assembly code");
2551
Nick Lewycky44d798d2011-10-17 23:05:28 +00002552 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002553 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002554 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002555
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002556 return false;
2557}
2558
2559/// ParseDirectiveLine
2560/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002561bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002562 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2563 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002564 return TokError("unexpected token in '.line' directive");
2565
Sean Callanan18b83232010-01-19 21:44:56 +00002566 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002567 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002568 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002569
2570 // FIXME: Do something with the .line.
2571 }
2572
Daniel Dunbareceec052010-07-12 17:45:27 +00002573 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002574 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002575
2576 return false;
2577}
2578
2579
2580/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002581/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002582/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2583/// The first number is a file number, must have been previously assigned with
2584/// a .file directive, the second number is the line number and optionally the
2585/// third number is a column position (zero if not specified). The remaining
2586/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002587bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002588
Daniel Dunbareceec052010-07-12 17:45:27 +00002589 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002590 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002591 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002592 if (FileNumber < 1)
2593 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002594 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002595 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002596 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002597
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002598 int64_t LineNumber = 0;
2599 if (getLexer().is(AsmToken::Integer)) {
2600 LineNumber = getTok().getIntVal();
2601 if (LineNumber < 1)
2602 return TokError("line number less than one in '.loc' directive");
2603 Lex();
2604 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002605
2606 int64_t ColumnPos = 0;
2607 if (getLexer().is(AsmToken::Integer)) {
2608 ColumnPos = getTok().getIntVal();
2609 if (ColumnPos < 0)
2610 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002611 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002612 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002613
Kevin Enderbyc0957932010-09-30 16:52:03 +00002614 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002615 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002616 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002617 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2618 for (;;) {
2619 if (getLexer().is(AsmToken::EndOfStatement))
2620 break;
2621
2622 StringRef Name;
2623 SMLoc Loc = getTok().getLoc();
2624 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002625 return TokError("unexpected token in '.loc' directive");
2626
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002627 if (Name == "basic_block")
2628 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2629 else if (Name == "prologue_end")
2630 Flags |= DWARF2_FLAG_PROLOGUE_END;
2631 else if (Name == "epilogue_begin")
2632 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2633 else if (Name == "is_stmt") {
2634 SMLoc Loc = getTok().getLoc();
2635 const MCExpr *Value;
2636 if (getParser().ParseExpression(Value))
2637 return true;
2638 // The expression must be the constant 0 or 1.
2639 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2640 int Value = MCE->getValue();
2641 if (Value == 0)
2642 Flags &= ~DWARF2_FLAG_IS_STMT;
2643 else if (Value == 1)
2644 Flags |= DWARF2_FLAG_IS_STMT;
2645 else
2646 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002647 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002648 else {
2649 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2650 }
2651 }
2652 else if (Name == "isa") {
2653 SMLoc Loc = getTok().getLoc();
2654 const MCExpr *Value;
2655 if (getParser().ParseExpression(Value))
2656 return true;
2657 // The expression must be a constant greater or equal to 0.
2658 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2659 int Value = MCE->getValue();
2660 if (Value < 0)
2661 return Error(Loc, "isa number less than zero");
2662 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002663 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002664 else {
2665 return Error(Loc, "isa number not a constant value");
2666 }
2667 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002668 else if (Name == "discriminator") {
2669 if (getParser().ParseAbsoluteExpression(Discriminator))
2670 return true;
2671 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002672 else {
2673 return Error(Loc, "unknown sub-directive in '.loc' directive");
2674 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002675
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002676 if (getLexer().is(AsmToken::EndOfStatement))
2677 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002678 }
2679 }
2680
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002681 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002682 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002683
2684 return false;
2685}
2686
Daniel Dunbar138abae2010-10-16 04:56:42 +00002687/// ParseDirectiveStabs
2688/// ::= .stabs string, number, number, number
2689bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2690 SMLoc DirectiveLoc) {
2691 return TokError("unsupported directive '" + Directive + "'");
2692}
2693
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002694/// ParseDirectiveCFISections
2695/// ::= .cfi_sections section [, section]
2696bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2697 SMLoc DirectiveLoc) {
2698 StringRef Name;
2699 bool EH = false;
2700 bool Debug = false;
2701
2702 if (getParser().ParseIdentifier(Name))
2703 return TokError("Expected an identifier");
2704
2705 if (Name == ".eh_frame")
2706 EH = true;
2707 else if (Name == ".debug_frame")
2708 Debug = true;
2709
2710 if (getLexer().is(AsmToken::Comma)) {
2711 Lex();
2712
2713 if (getParser().ParseIdentifier(Name))
2714 return TokError("Expected an identifier");
2715
2716 if (Name == ".eh_frame")
2717 EH = true;
2718 else if (Name == ".debug_frame")
2719 Debug = true;
2720 }
2721
2722 getStreamer().EmitCFISections(EH, Debug);
2723
2724 return false;
2725}
2726
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002727/// ParseDirectiveCFIStartProc
2728/// ::= .cfi_startproc
2729bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2730 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002731 getStreamer().EmitCFIStartProc();
2732 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002733}
2734
2735/// ParseDirectiveCFIEndProc
2736/// ::= .cfi_endproc
2737bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002738 getStreamer().EmitCFIEndProc();
2739 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002740}
2741
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002742/// ParseRegisterOrRegisterNumber - parse register name or number.
2743bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2744 SMLoc DirectiveLoc) {
2745 unsigned RegNo;
2746
Jim Grosbach6f888a82011-06-02 17:14:04 +00002747 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002748 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2749 DirectiveLoc))
2750 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002751 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002752 } else
2753 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002754
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002755 return false;
2756}
2757
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002758/// ParseDirectiveCFIDefCfa
2759/// ::= .cfi_def_cfa register, offset
2760bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2761 SMLoc DirectiveLoc) {
2762 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002763 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002764 return true;
2765
2766 if (getLexer().isNot(AsmToken::Comma))
2767 return TokError("unexpected token in directive");
2768 Lex();
2769
2770 int64_t Offset = 0;
2771 if (getParser().ParseAbsoluteExpression(Offset))
2772 return true;
2773
Rafael Espindola066c2f42011-04-12 23:59:07 +00002774 getStreamer().EmitCFIDefCfa(Register, Offset);
2775 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002776}
2777
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002778/// ParseDirectiveCFIDefCfaOffset
2779/// ::= .cfi_def_cfa_offset offset
2780bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2781 SMLoc DirectiveLoc) {
2782 int64_t Offset = 0;
2783 if (getParser().ParseAbsoluteExpression(Offset))
2784 return true;
2785
Rafael Espindola066c2f42011-04-12 23:59:07 +00002786 getStreamer().EmitCFIDefCfaOffset(Offset);
2787 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002788}
2789
2790/// ParseDirectiveCFIAdjustCfaOffset
2791/// ::= .cfi_adjust_cfa_offset adjustment
2792bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2793 SMLoc DirectiveLoc) {
2794 int64_t Adjustment = 0;
2795 if (getParser().ParseAbsoluteExpression(Adjustment))
2796 return true;
2797
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002798 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2799 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002800}
2801
2802/// ParseDirectiveCFIDefCfaRegister
2803/// ::= .cfi_def_cfa_register register
2804bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2805 SMLoc DirectiveLoc) {
2806 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002807 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002808 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002809
Rafael Espindola066c2f42011-04-12 23:59:07 +00002810 getStreamer().EmitCFIDefCfaRegister(Register);
2811 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002812}
2813
2814/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002815/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002816bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2817 int64_t Register = 0;
2818 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002819
2820 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002821 return true;
2822
2823 if (getLexer().isNot(AsmToken::Comma))
2824 return TokError("unexpected token in directive");
2825 Lex();
2826
2827 if (getParser().ParseAbsoluteExpression(Offset))
2828 return true;
2829
Rafael Espindola066c2f42011-04-12 23:59:07 +00002830 getStreamer().EmitCFIOffset(Register, Offset);
2831 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002832}
2833
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002834/// ParseDirectiveCFIRelOffset
2835/// ::= .cfi_rel_offset register, offset
2836bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2837 SMLoc DirectiveLoc) {
2838 int64_t Register = 0;
2839
2840 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2841 return true;
2842
2843 if (getLexer().isNot(AsmToken::Comma))
2844 return TokError("unexpected token in directive");
2845 Lex();
2846
2847 int64_t Offset = 0;
2848 if (getParser().ParseAbsoluteExpression(Offset))
2849 return true;
2850
Rafael Espindola25f492e2011-04-12 16:12:03 +00002851 getStreamer().EmitCFIRelOffset(Register, Offset);
2852 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002853}
2854
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002855static bool isValidEncoding(int64_t Encoding) {
2856 if (Encoding & ~0xff)
2857 return false;
2858
2859 if (Encoding == dwarf::DW_EH_PE_omit)
2860 return true;
2861
2862 const unsigned Format = Encoding & 0xf;
2863 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2864 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2865 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2866 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2867 return false;
2868
Rafael Espindolacaf11582010-12-29 04:31:26 +00002869 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002870 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002871 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002872 return false;
2873
2874 return true;
2875}
2876
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002877/// ParseDirectiveCFIPersonalityOrLsda
2878/// ::= .cfi_personality encoding, [symbol_name]
2879/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002880bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002881 SMLoc DirectiveLoc) {
2882 int64_t Encoding = 0;
2883 if (getParser().ParseAbsoluteExpression(Encoding))
2884 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002885 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002886 return false;
2887
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002888 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002889 return TokError("unsupported encoding.");
2890
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002891 if (getLexer().isNot(AsmToken::Comma))
2892 return TokError("unexpected token in directive");
2893 Lex();
2894
2895 StringRef Name;
2896 if (getParser().ParseIdentifier(Name))
2897 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002898
2899 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2900
2901 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002902 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002903 else {
2904 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002905 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002906 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002907 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002908}
2909
Rafael Espindolafe024d02010-12-28 18:36:23 +00002910/// ParseDirectiveCFIRememberState
2911/// ::= .cfi_remember_state
2912bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2913 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002914 getStreamer().EmitCFIRememberState();
2915 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002916}
2917
2918/// ParseDirectiveCFIRestoreState
2919/// ::= .cfi_remember_state
2920bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2921 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002922 getStreamer().EmitCFIRestoreState();
2923 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002924}
2925
Rafael Espindolac5754392011-04-12 15:31:05 +00002926/// ParseDirectiveCFISameValue
2927/// ::= .cfi_same_value register
2928bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2929 SMLoc DirectiveLoc) {
2930 int64_t Register = 0;
2931
2932 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2933 return true;
2934
2935 getStreamer().EmitCFISameValue(Register);
2936
2937 return false;
2938}
2939
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00002940/// ParseDirectiveCFIRestore
2941/// ::= .cfi_restore register
2942bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
2943 SMLoc DirectiveLoc) {
2944 int64_t Register = 0;
2945 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2946 return true;
2947
2948 getStreamer().EmitCFIRestore(Register);
2949
2950 return false;
2951}
2952
Rafael Espindola6f0b1812011-12-29 20:24:47 +00002953/// ParseDirectiveCFIEscape
2954/// ::= .cfi_escape expression[,...]
2955bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
2956 SMLoc DirectiveLoc) {
2957 std::string Values;
2958 int64_t CurrValue;
2959 if (getParser().ParseAbsoluteExpression(CurrValue))
2960 return true;
2961
2962 Values.push_back((uint8_t)CurrValue);
2963
2964 while (getLexer().is(AsmToken::Comma)) {
2965 Lex();
2966
2967 if (getParser().ParseAbsoluteExpression(CurrValue))
2968 return true;
2969
2970 Values.push_back((uint8_t)CurrValue);
2971 }
2972
2973 getStreamer().EmitCFIEscape(Values);
2974 return false;
2975}
2976
Rafael Espindola16d7d432012-01-23 21:51:52 +00002977/// ParseDirectiveCFISignalFrame
2978/// ::= .cfi_signal_frame
2979bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
2980 SMLoc DirectiveLoc) {
2981 if (getLexer().isNot(AsmToken::EndOfStatement))
2982 return Error(getLexer().getLoc(),
2983 "unexpected token in '" + Directive + "' directive");
2984
2985 getStreamer().EmitCFISignalFrame();
2986
2987 return false;
2988}
2989
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002990/// ParseDirectiveMacrosOnOff
2991/// ::= .macros_on
2992/// ::= .macros_off
2993bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2994 SMLoc DirectiveLoc) {
2995 if (getLexer().isNot(AsmToken::EndOfStatement))
2996 return Error(getLexer().getLoc(),
2997 "unexpected token in '" + Directive + "' directive");
2998
2999 getParser().MacrosEnabled = Directive == ".macros_on";
3000
3001 return false;
3002}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003003
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003004/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003005/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003006bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3007 SMLoc DirectiveLoc) {
3008 StringRef Name;
3009 if (getParser().ParseIdentifier(Name))
3010 return TokError("expected identifier in directive");
3011
Rafael Espindola65366442011-06-05 02:43:45 +00003012 std::vector<StringRef> Parameters;
3013 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3014 for(;;) {
3015 StringRef Parameter;
3016 if (getParser().ParseIdentifier(Parameter))
3017 return TokError("expected identifier in directive");
3018 Parameters.push_back(Parameter);
3019
3020 if (getLexer().isNot(AsmToken::Comma))
3021 break;
3022 Lex();
3023 }
3024 }
3025
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003026 if (getLexer().isNot(AsmToken::EndOfStatement))
3027 return TokError("unexpected token in '.macro' directive");
3028
3029 // Eat the end of statement.
3030 Lex();
3031
3032 AsmToken EndToken, StartToken = getTok();
3033
3034 // Lex the macro definition.
3035 for (;;) {
3036 // Check whether we have reached the end of the file.
3037 if (getLexer().is(AsmToken::Eof))
3038 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3039
3040 // Otherwise, check whether we have reach the .endmacro.
3041 if (getLexer().is(AsmToken::Identifier) &&
3042 (getTok().getIdentifier() == ".endm" ||
3043 getTok().getIdentifier() == ".endmacro")) {
3044 EndToken = getTok();
3045 Lex();
3046 if (getLexer().isNot(AsmToken::EndOfStatement))
3047 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3048 "' directive");
3049 break;
3050 }
3051
3052 // Otherwise, scan til the end of the statement.
3053 getParser().EatToEndOfStatement();
3054 }
3055
3056 if (getParser().MacroMap.lookup(Name)) {
3057 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3058 }
3059
3060 const char *BodyStart = StartToken.getLoc().getPointer();
3061 const char *BodyEnd = EndToken.getLoc().getPointer();
3062 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003063 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003064 return false;
3065}
3066
3067/// ParseDirectiveEndMacro
3068/// ::= .endm
3069/// ::= .endmacro
3070bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
3071 SMLoc DirectiveLoc) {
3072 if (getLexer().isNot(AsmToken::EndOfStatement))
3073 return TokError("unexpected token in '" + Directive + "' directive");
3074
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003075 // If we are inside a macro instantiation, terminate the current
3076 // instantiation.
3077 if (!getParser().ActiveMacros.empty()) {
3078 getParser().HandleMacroExit();
3079 return false;
3080 }
3081
3082 // Otherwise, this .endmacro is a stray entry in the file; well formed
3083 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003084 return TokError("unexpected '" + Directive + "' in file, "
3085 "no current macro definition");
3086}
3087
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003088/// ParseDirectivePurgeMacro
3089/// ::= .purgem
3090bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3091 SMLoc DirectiveLoc) {
3092 StringRef Name;
3093 if (getParser().ParseIdentifier(Name))
3094 return TokError("expected identifier in '.purgem' directive");
3095
3096 if (getLexer().isNot(AsmToken::EndOfStatement))
3097 return TokError("unexpected token in '.purgem' directive");
3098
3099 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3100 if (I == getParser().MacroMap.end())
3101 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3102
3103 // Undefine the macro.
3104 delete I->getValue();
3105 getParser().MacroMap.erase(I);
3106 return false;
3107}
3108
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003109bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003110 getParser().CheckForValidSection();
3111
3112 const MCExpr *Value;
3113
3114 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003115 return true;
3116
3117 if (getLexer().isNot(AsmToken::EndOfStatement))
3118 return TokError("unexpected token in directive");
3119
3120 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003121 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003122 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003123 getStreamer().EmitULEB128Value(Value);
3124
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003125 return false;
3126}
3127
3128
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003129/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003130MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003131 MCContext &C, MCStreamer &Out,
3132 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003133 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003134}