blob: 8ac0fd244c0b3ca3311c461450282ccf7222ee36 [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");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000339
340 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
341 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000342 }
343
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000344 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
345
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000346 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
347 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
348 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000349 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000350 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000351 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
352 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000353 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000354 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000355 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000356 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
357 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000358 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000359 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000360 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
361 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000362 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000363 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000364 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000365 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000366
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000367 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000368 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
369 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000370
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000371 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000372};
373
374}
375
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000376namespace llvm {
377
378extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000379extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000380extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000381
382}
383
Chris Lattneraaec2052010-01-19 19:46:13 +0000384enum { DEFAULT_ADDRSPACE = 0 };
385
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000386AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000387 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000388 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000389 GenericParser(new GenericAsmParser), PlatformParser(0),
Devang Patel0db58bf2012-01-31 18:14:05 +0000390 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
391 AssemblerDialect(~0U) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000392 // Save the old handler.
393 SavedDiagHandler = SrcMgr.getDiagHandler();
394 SavedDiagContext = SrcMgr.getDiagContext();
395 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000396 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000397 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000398
399 // Initialize the generic parser.
400 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000401
402 // Initialize the platform / file format parser.
403 //
404 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
405 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000406 if (_MAI.hasMicrosoftFastStdCallMangling()) {
407 PlatformParser = createCOFFAsmParser();
408 PlatformParser->Initialize(*this);
409 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000410 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000411 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000412 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000413 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000414 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000415 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000416}
417
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000418AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000419 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
420
421 // Destroy any macros.
422 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
423 ie = MacroMap.end(); it != ie; ++it)
424 delete it->getValue();
425
Daniel Dunbare4749702010-07-12 18:12:02 +0000426 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000427 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000428}
429
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000430void AsmParser::PrintMacroInstantiations() {
431 // Print the active macro instantiation stack.
432 for (std::vector<MacroInstantiation*>::const_reverse_iterator
433 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000434 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
435 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000436}
437
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000438bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000439 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000440 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000441 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000442 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000443 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000444}
445
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000446bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000447 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000448 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000449 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000450 return true;
451}
452
Sean Callananfd0b0282010-01-21 00:19:58 +0000453bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000454 std::string IncludedFile;
455 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000456 if (NewBuf == -1)
457 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000458
Sean Callananfd0b0282010-01-21 00:19:58 +0000459 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000460
Sean Callananfd0b0282010-01-21 00:19:58 +0000461 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000462
Sean Callananfd0b0282010-01-21 00:19:58 +0000463 return false;
464}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000465
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000466/// Process the specified .incbin file by seaching for it in the include paths
467/// then just emiting the byte contents of the file to the streamer. This
468/// returns true on failure.
469bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
470 std::string IncludedFile;
471 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
472 if (NewBuf == -1)
473 return true;
474
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000475 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000476 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
477 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000478 return false;
479}
480
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000481void AsmParser::JumpToLoc(SMLoc Loc) {
482 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
483 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
484}
485
Sean Callananfd0b0282010-01-21 00:19:58 +0000486const AsmToken &AsmParser::Lex() {
487 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000488
Sean Callananfd0b0282010-01-21 00:19:58 +0000489 if (tok->is(AsmToken::Eof)) {
490 // If this is the end of an included file, pop the parent file off the
491 // include stack.
492 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
493 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000494 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000495 tok = &Lexer.Lex();
496 }
497 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000498
Sean Callananfd0b0282010-01-21 00:19:58 +0000499 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000500 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000501
Sean Callananfd0b0282010-01-21 00:19:58 +0000502 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000503}
504
Chris Lattner79180e22010-04-05 23:15:42 +0000505bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000506 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000507 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000508 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000509
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000510 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000511 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000512
513 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000514 AsmCond StartingCondState = TheCondState;
515
Kevin Enderby613b7572011-11-01 22:27:22 +0000516 // If we are generating dwarf for assembly source files save the initial text
517 // section and generate a .file directive.
518 if (getContext().getGenDwarfForAssembly()) {
519 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000520 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
521 getStreamer().EmitLabel(SectionStartSym);
522 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000523 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
524 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
525 }
526
Chris Lattnerb717fb02009-07-02 21:53:43 +0000527 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000528 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000529 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000530
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000531 // We had an error, validate that one was emitted and recover by skipping to
532 // the next line.
533 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000534 EatToEndOfStatement();
535 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000536
537 if (TheCondState.TheCond != StartingCondState.TheCond ||
538 TheCondState.Ignore != StartingCondState.Ignore)
539 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000540
541 // Check to see there are no empty DwarfFile slots.
542 const std::vector<MCDwarfFile *> &MCDwarfFiles =
543 getContext().getMCDwarfFiles();
544 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000545 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000546 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000547 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000548
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000549 // Check to see that all assembler local symbols were actually defined.
550 // Targets that don't do subsections via symbols may not want this, though,
551 // so conservatively exclude them. Only do this if we're finalizing, though,
552 // as otherwise we won't necessarilly have seen everything yet.
553 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
554 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
555 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
556 e = Symbols.end();
557 i != e; ++i) {
558 MCSymbol *Sym = i->getValue();
559 // Variable symbols may not be marked as defined, so check those
560 // explicitly. If we know it's a variable, we have a definition for
561 // the purposes of this check.
562 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
563 // FIXME: We would really like to refer back to where the symbol was
564 // first referenced for a source location. We need to add something
565 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000566 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
567 "assembler local symbol '" + Sym->getName() +
568 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000569 }
570 }
571
572
Chris Lattner79180e22010-04-05 23:15:42 +0000573 // Finalize the output stream if there are no errors and if the client wants
574 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000575 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000576 Out.Finish();
577
Chris Lattnerb717fb02009-07-02 21:53:43 +0000578 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000579}
580
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000581void AsmParser::CheckForValidSection() {
582 if (!getStreamer().getCurrentSection()) {
583 TokError("expected section directive before assembly directive");
584 Out.SwitchSection(Ctx.getMachOSection(
585 "__TEXT", "__text",
586 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
587 0, SectionKind::getText()));
588 }
589}
590
Chris Lattner2cf5f142009-06-22 01:29:09 +0000591/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
592void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000593 while (Lexer.isNot(AsmToken::EndOfStatement) &&
594 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000595 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000596
Chris Lattner2cf5f142009-06-22 01:29:09 +0000597 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000598 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000599 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000600}
601
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000602StringRef AsmParser::ParseStringToEndOfStatement() {
603 const char *Start = getTok().getLoc().getPointer();
604
605 while (Lexer.isNot(AsmToken::EndOfStatement) &&
606 Lexer.isNot(AsmToken::Eof))
607 Lex();
608
609 const char *End = getTok().getLoc().getPointer();
610 return StringRef(Start, End - Start);
611}
Chris Lattnerc4193832009-06-22 05:51:26 +0000612
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000613StringRef AsmParser::ParseStringToComma() {
614 const char *Start = getTok().getLoc().getPointer();
615
616 while (Lexer.isNot(AsmToken::EndOfStatement) &&
617 Lexer.isNot(AsmToken::Comma) &&
618 Lexer.isNot(AsmToken::Eof))
619 Lex();
620
621 const char *End = getTok().getLoc().getPointer();
622 return StringRef(Start, End - Start);
623}
624
Chris Lattner74ec1a32009-06-22 06:32:03 +0000625/// ParseParenExpr - Parse a paren expression and return it.
626/// NOTE: This assumes the leading '(' has already been consumed.
627///
628/// parenexpr ::= expr)
629///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000630bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000631 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000632 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000633 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000634 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000635 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000636 return false;
637}
Chris Lattnerc4193832009-06-22 05:51:26 +0000638
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000639/// ParseBracketExpr - Parse a bracket expression and return it.
640/// NOTE: This assumes the leading '[' has already been consumed.
641///
642/// bracketexpr ::= expr]
643///
644bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
645 if (ParseExpression(Res)) return true;
646 if (Lexer.isNot(AsmToken::RBrac))
647 return TokError("expected ']' in brackets expression");
648 EndLoc = Lexer.getLoc();
649 Lex();
650 return false;
651}
652
Chris Lattner74ec1a32009-06-22 06:32:03 +0000653/// ParsePrimaryExpr - Parse a primary expression and return it.
654/// primaryexpr ::= (parenexpr
655/// primaryexpr ::= symbol
656/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000657/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000658/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000659bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000660 switch (Lexer.getKind()) {
661 default:
662 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000663 // If we have an error assume that we've already handled it.
664 case AsmToken::Error:
665 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000666 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000667 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000668 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000669 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000670 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000671 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000672 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000673 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000674 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000675 EndLoc = Lexer.getLoc();
676
677 StringRef Identifier;
678 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000679 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000680
Daniel Dunbarfffff912009-10-16 01:34:54 +0000681 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000682 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000683 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000684
685 // Lookup the symbol variant if used.
686 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000687 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000688 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000689 if (Variant == MCSymbolRefExpr::VK_Invalid) {
690 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000691 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000692 }
693 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000694
Daniel Dunbarfffff912009-10-16 01:34:54 +0000695 // If this is an absolute variable reference, substitute it now to preserve
696 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000697 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000698 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000699 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000700
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000701 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000702 return false;
703 }
704
705 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000706 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000707 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000708 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000709 case AsmToken::Integer: {
710 SMLoc Loc = getTok().getLoc();
711 int64_t IntVal = getTok().getIntVal();
712 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000713 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000714 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000715 // Look for 'b' or 'f' following an Integer as a directional label
716 if (Lexer.getKind() == AsmToken::Identifier) {
717 StringRef IDVal = getTok().getString();
718 if (IDVal == "f" || IDVal == "b"){
719 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
720 IDVal == "f" ? 1 : 0);
721 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
722 getContext());
723 if(IDVal == "b" && Sym->isUndefined())
724 return Error(Loc, "invalid reference to undefined symbol");
725 EndLoc = Lexer.getLoc();
726 Lex(); // Eat identifier.
727 }
728 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000729 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000730 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000731 case AsmToken::Real: {
732 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000733 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000734 Res = MCConstantExpr::Create(IntVal, getContext());
735 Lex(); // Eat token.
736 return false;
737 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000738 case AsmToken::Dot: {
739 // This is a '.' reference, which references the current PC. Emit a
740 // temporary label to the streamer and refer to it.
741 MCSymbol *Sym = Ctx.CreateTempSymbol();
742 Out.EmitLabel(Sym);
743 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
744 EndLoc = Lexer.getLoc();
745 Lex(); // Eat identifier.
746 return false;
747 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000748 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000749 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000750 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000751 case AsmToken::LBrac:
752 if (!PlatformParser->HasBracketExpressions())
753 return TokError("brackets expression not supported on this target");
754 Lex(); // Eat the '['.
755 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000756 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000757 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000758 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000759 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000760 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000761 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000762 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000763 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000764 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000765 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000766 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000767 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000768 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000769 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000770 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000771 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000772 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000773 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000774 }
775}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000776
Chris Lattnerb4307b32010-01-15 19:28:38 +0000777bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000778 SMLoc EndLoc;
779 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000780}
781
Daniel Dunbarcceba832010-09-17 02:47:07 +0000782const MCExpr *
783AsmParser::ApplyModifierToExpr(const MCExpr *E,
784 MCSymbolRefExpr::VariantKind Variant) {
785 // Recurse over the given expression, rebuilding it to apply the given variant
786 // if there is exactly one symbol.
787 switch (E->getKind()) {
788 case MCExpr::Target:
789 case MCExpr::Constant:
790 return 0;
791
792 case MCExpr::SymbolRef: {
793 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
794
795 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
796 TokError("invalid variant on expression '" +
797 getTok().getIdentifier() + "' (already modified)");
798 return E;
799 }
800
801 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
802 }
803
804 case MCExpr::Unary: {
805 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
806 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
807 if (!Sub)
808 return 0;
809 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
810 }
811
812 case MCExpr::Binary: {
813 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
814 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
815 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
816
817 if (!LHS && !RHS)
818 return 0;
819
820 if (!LHS) LHS = BE->getLHS();
821 if (!RHS) RHS = BE->getRHS();
822
823 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
824 }
825 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000826
Craig Topper85814382012-02-07 05:05:23 +0000827 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000828}
829
Chris Lattner74ec1a32009-06-22 06:32:03 +0000830/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000831///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000832/// expr ::= expr &&,|| expr -> lowest.
833/// expr ::= expr |,^,&,! expr
834/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
835/// expr ::= expr <<,>> expr
836/// expr ::= expr +,- expr
837/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000838/// expr ::= primaryexpr
839///
Chris Lattner54482b42010-01-15 19:39:23 +0000840bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000841 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000842 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000843 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
844 return true;
845
Daniel Dunbarcceba832010-09-17 02:47:07 +0000846 // As a special case, we support 'a op b @ modifier' by rewriting the
847 // expression to include the modifier. This is inefficient, but in general we
848 // expect users to use 'a@modifier op b'.
849 if (Lexer.getKind() == AsmToken::At) {
850 Lex();
851
852 if (Lexer.isNot(AsmToken::Identifier))
853 return TokError("unexpected symbol modifier following '@'");
854
855 MCSymbolRefExpr::VariantKind Variant =
856 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
857 if (Variant == MCSymbolRefExpr::VK_Invalid)
858 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
859
860 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
861 if (!ModifiedRes) {
862 return TokError("invalid modifier '" + getTok().getIdentifier() +
863 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000864 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000865
Daniel Dunbarcceba832010-09-17 02:47:07 +0000866 Res = ModifiedRes;
867 Lex();
868 }
869
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000870 // Try to constant fold it up front, if possible.
871 int64_t Value;
872 if (Res->EvaluateAsAbsolute(Value))
873 Res = MCConstantExpr::Create(Value, getContext());
874
875 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000876}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000877
Chris Lattnerb4307b32010-01-15 19:28:38 +0000878bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000879 Res = 0;
880 return ParseParenExpr(Res, EndLoc) ||
881 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000882}
883
Daniel Dunbar475839e2009-06-29 20:37:27 +0000884bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000885 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000886
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000887 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000888 if (ParseExpression(Expr))
889 return true;
890
Daniel Dunbare00b0112009-10-16 01:57:52 +0000891 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000892 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000893
894 return false;
895}
896
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000897static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000898 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000899 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000900 default:
901 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000902
Jim Grosbachfbe16812011-08-20 16:24:13 +0000903 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000904 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000905 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000906 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000907 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000908 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000909 return 1;
910
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000911
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000912 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000913 //
914 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000915 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000916 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000917 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000918 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000919 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000920 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000921 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000922 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000923 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000924
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000925 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000926 case AsmToken::EqualEqual:
927 Kind = MCBinaryExpr::EQ;
928 return 3;
929 case AsmToken::ExclaimEqual:
930 case AsmToken::LessGreater:
931 Kind = MCBinaryExpr::NE;
932 return 3;
933 case AsmToken::Less:
934 Kind = MCBinaryExpr::LT;
935 return 3;
936 case AsmToken::LessEqual:
937 Kind = MCBinaryExpr::LTE;
938 return 3;
939 case AsmToken::Greater:
940 Kind = MCBinaryExpr::GT;
941 return 3;
942 case AsmToken::GreaterEqual:
943 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000944 return 3;
945
Jim Grosbachfbe16812011-08-20 16:24:13 +0000946 // Intermediate Precedence: <<, >>
947 case AsmToken::LessLess:
948 Kind = MCBinaryExpr::Shl;
949 return 4;
950 case AsmToken::GreaterGreater:
951 Kind = MCBinaryExpr::Shr;
952 return 4;
953
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000954 // High Intermediate Precedence: +, -
955 case AsmToken::Plus:
956 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000957 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000958 case AsmToken::Minus:
959 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000960 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000961
Jim Grosbachfbe16812011-08-20 16:24:13 +0000962 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000963 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000964 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000965 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000966 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000967 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000968 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000969 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000970 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000971 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000972 }
973}
974
975
976/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
977/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000978bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
979 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000980 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000981 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000982 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000983
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000984 // If the next token is lower precedence than we are allowed to eat, return
985 // successfully with what we ate already.
986 if (TokPrec < Precedence)
987 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000988
Sean Callanan79ed1a82010-01-19 20:22:31 +0000989 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000990
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000991 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000992 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000993 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000994
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000995 // If BinOp binds less tightly with RHS than the operator after RHS, let
996 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000997 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000998 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000999 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001000 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001001 }
1002
Daniel Dunbar475839e2009-06-29 20:37:27 +00001003 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001004 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001005 }
1006}
1007
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001008
1009
1010
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001011/// ParseStatement:
1012/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001013/// ::= Label* Directive ...Operands... EndOfStatement
1014/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001015bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001016 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001017 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001018 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001019 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001020 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001021
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001022 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001023 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001024 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001025 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001026 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001027 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001028 if (Lexer.is(AsmToken::Hash))
1029 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001030
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001031 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001032 if (Lexer.is(AsmToken::Integer)) {
1033 LocalLabelVal = getTok().getIntVal();
1034 if (LocalLabelVal < 0) {
1035 if (!TheCondState.Ignore)
1036 return TokError("unexpected token at start of statement");
1037 IDVal = "";
1038 }
1039 else {
1040 IDVal = getTok().getString();
1041 Lex(); // Consume the integer token to be used as an identifier token.
1042 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001043 if (!TheCondState.Ignore)
1044 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001045 }
1046 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001047
1048 } else if (Lexer.is(AsmToken::Dot)) {
1049 // Treat '.' as a valid identifier in this context.
1050 Lex();
1051 IDVal = ".";
1052
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001053 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001054 if (!TheCondState.Ignore)
1055 return TokError("unexpected token at start of statement");
1056 IDVal = "";
1057 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001058
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001059
Chris Lattner7834fac2010-04-17 18:14:27 +00001060 // Handle conditional assembly here before checking for skipping. We
1061 // have to do this so that .endif isn't skipped in a ".if 0" block for
1062 // example.
1063 if (IDVal == ".if")
1064 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001065 if (IDVal == ".ifb")
1066 return ParseDirectiveIfb(IDLoc, true);
1067 if (IDVal == ".ifnb")
1068 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00001069 if (IDVal == ".ifc")
1070 return ParseDirectiveIfc(IDLoc, true);
1071 if (IDVal == ".ifnc")
1072 return ParseDirectiveIfc(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001073 if (IDVal == ".ifdef")
1074 return ParseDirectiveIfdef(IDLoc, true);
1075 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1076 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001077 if (IDVal == ".elseif")
1078 return ParseDirectiveElseIf(IDLoc);
1079 if (IDVal == ".else")
1080 return ParseDirectiveElse(IDLoc);
1081 if (IDVal == ".endif")
1082 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001083
Chris Lattner7834fac2010-04-17 18:14:27 +00001084 // If we are in a ".if 0" block, ignore this statement.
1085 if (TheCondState.Ignore) {
1086 EatToEndOfStatement();
1087 return false;
1088 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001089
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001090 // FIXME: Recurse on local labels?
1091
1092 // See what kind of statement we have.
1093 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001094 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001095 CheckForValidSection();
1096
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001097 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001098 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001099
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001100 // Diagnose attempt to use '.' as a label.
1101 if (IDVal == ".")
1102 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1103
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001104 // Diagnose attempt to use a variable as a label.
1105 //
1106 // FIXME: Diagnostics. Note the location of the definition as a label.
1107 // FIXME: This doesn't diagnose assignment to a symbol which has been
1108 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001109 MCSymbol *Sym;
1110 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001111 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001112 else
1113 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001114 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001115 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001116
Daniel Dunbar959fd882009-08-26 22:13:22 +00001117 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001118 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001119
Kevin Enderby94c2e852011-12-09 18:09:40 +00001120 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001121 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001122 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001123 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1124 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001125
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001126 // Consume any end of statement token, if present, to avoid spurious
1127 // AddBlankLine calls().
1128 if (Lexer.is(AsmToken::EndOfStatement)) {
1129 Lex();
1130 if (Lexer.is(AsmToken::Eof))
1131 return false;
1132 }
1133
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001134 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001135 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001136
Daniel Dunbar3f872332009-07-28 16:08:33 +00001137 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001138 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001139 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001140
Nico Weber4c4c7322011-01-28 03:04:41 +00001141 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001142
1143 default: // Normal instruction or directive.
1144 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001145 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001146
1147 // If macros are enabled, check to see if this is a macro instantiation.
1148 if (MacrosEnabled)
1149 if (const Macro *M = MacroMap.lookup(IDVal))
1150 return HandleMacroEntry(IDVal, IDLoc, M);
1151
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001152 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001153 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001154 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001155 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001156 return ParseDirectiveSet(IDVal, true);
1157 if (IDVal == ".equiv")
1158 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001159
Daniel Dunbara0d14262009-06-24 23:30:00 +00001160 // Data directives
1161
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001162 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001163 return ParseDirectiveAscii(IDVal, false);
1164 if (IDVal == ".asciz" || IDVal == ".string")
1165 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001166
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001167 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001168 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001169 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001170 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001171 if (IDVal == ".value")
1172 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001173 if (IDVal == ".2byte")
1174 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001175 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001176 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001177 if (IDVal == ".int")
1178 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001179 if (IDVal == ".4byte")
1180 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001181 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001182 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001183 if (IDVal == ".8byte")
1184 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001185 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001186 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1187 if (IDVal == ".double")
1188 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001189
Eli Friedman5d68ec22010-07-19 04:17:25 +00001190 if (IDVal == ".align") {
1191 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1192 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1193 }
1194 if (IDVal == ".align32") {
1195 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1196 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1197 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001198 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001199 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001200 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001201 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001202 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001203 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001204 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001205 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001206 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001207 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001208 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001209 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1210
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001211 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001212 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001213
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001214 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001215 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001216 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001217 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001218 if (IDVal == ".zero")
1219 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001220
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001221 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001222
Benjamin Kramere14a3c52012-05-12 11:18:59 +00001223 if (IDVal == ".extern") {
1224 EatToEndOfStatement(); // .extern is the default, ignore it.
1225 return false;
1226 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001227 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001228 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001229 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001230 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001231 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001232 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001233 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001234 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001235 if (IDVal == ".symbol_resolver")
1236 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001237 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001238 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001239 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001240 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001241 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001242 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001243 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001244 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001245 if (IDVal == ".weak_def_can_be_hidden")
1246 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001247
Hans Wennborg5cc64912011-06-18 13:51:54 +00001248 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001249 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001250 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001251 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001252
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001253 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001254 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001255 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001256 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001257 if (IDVal == ".incbin")
1258 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001259
Benjamin Kramer5cdf0ad2012-05-12 11:19:04 +00001260 if (IDVal == ".code16" || IDVal == ".code16gcc")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001261 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001262
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001263 // Look up the handler in the handler table.
1264 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1265 DirectiveMap.lookup(IDVal);
1266 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001267 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001268
Kevin Enderby9c656452009-09-10 20:51:44 +00001269 // Target hook for parsing target specific directives.
1270 if (!getTargetParser().ParseDirective(ID))
1271 return false;
1272
Jim Grosbach686c0182012-05-01 18:38:27 +00001273 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001274 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001275
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001276 CheckForValidSection();
1277
Chris Lattnera7f13542010-05-19 23:34:33 +00001278 // Canonicalize the opcode to lower case.
1279 SmallString<128> Opcode;
1280 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1281 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001282
Chris Lattner98986712010-01-14 22:21:20 +00001283 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001284 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001285 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001286
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001287 // Dump the parsed representation, if requested.
1288 if (getShowParsedOperands()) {
1289 SmallString<256> Str;
1290 raw_svector_ostream OS(Str);
1291 OS << "parsed instruction: [";
1292 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1293 if (i != 0)
1294 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001295 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001296 }
1297 OS << "]";
1298
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001299 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001300 }
1301
Kevin Enderby613b7572011-11-01 22:27:22 +00001302 // If we are generating dwarf for assembly source files and the current
1303 // section is the initial text section then generate a .loc directive for
1304 // the instruction.
1305 if (!HadError && getContext().getGenDwarfForAssembly() &&
1306 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1307 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1308 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1309 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001310 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001311 StringRef());
1312 }
1313
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001314 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001315 if (!HadError)
1316 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1317 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001318
Chris Lattner98986712010-01-14 22:21:20 +00001319 // Free any parsed operands.
1320 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1321 delete ParsedOperands[i];
1322
Chris Lattnercbf8a982010-09-11 16:18:25 +00001323 // Don't skip the rest of the line, the instruction parser is responsible for
1324 // that.
1325 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001326}
Chris Lattner9a023f72009-06-24 04:43:34 +00001327
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001328/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1329/// since they may not be able to be tokenized to get to the end of line token.
1330void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001331 if (!Lexer.is(AsmToken::EndOfStatement))
1332 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001333 // Eat EOL.
1334 Lex();
1335}
1336
1337/// ParseCppHashLineFilenameComment as this:
1338/// ::= # number "filename"
1339/// or just as a full line comment if it doesn't have a number and a string.
1340bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1341 Lex(); // Eat the hash token.
1342
1343 if (getLexer().isNot(AsmToken::Integer)) {
1344 // Consume the line since in cases it is not a well-formed line directive,
1345 // as if were simply a full line comment.
1346 EatToEndOfLine();
1347 return false;
1348 }
1349
1350 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001351 Lex();
1352
1353 if (getLexer().isNot(AsmToken::String)) {
1354 EatToEndOfLine();
1355 return false;
1356 }
1357
1358 StringRef Filename = getTok().getString();
1359 // Get rid of the enclosing quotes.
1360 Filename = Filename.substr(1, Filename.size()-2);
1361
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001362 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1363 CppHashLoc = L;
1364 CppHashFilename = Filename;
1365 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001366
1367 // Ignore any trailing characters, they're just comment.
1368 EatToEndOfLine();
1369 return false;
1370}
1371
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001372/// DiagHandler - will use the the last parsed cpp hash line filename comment
1373/// for the Filename and LineNo if any in the diagnostic.
1374void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1375 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1376 raw_ostream &OS = errs();
1377
1378 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1379 const SMLoc &DiagLoc = Diag.getLoc();
1380 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1381 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1382
1383 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1384 // before printing the message.
1385 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001386 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001387 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1388 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1389 }
1390
1391 // If we have not parsed a cpp hash line filename comment or the source
1392 // manager changed or buffer changed (like in a nested include) then just
1393 // print the normal diagnostic using its Filename and LineNo.
1394 if (!Parser->CppHashLineNumber ||
1395 &DiagSrcMgr != &Parser->SrcMgr ||
1396 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001397 if (Parser->SavedDiagHandler)
1398 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1399 else
1400 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001401 return;
1402 }
1403
1404 // Use the CppHashFilename and calculate a line number based on the
1405 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1406 // the diagnostic.
1407 const std::string Filename = Parser->CppHashFilename;
1408
1409 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1410 int CppHashLocLineNo =
1411 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1412 int LineNo = Parser->CppHashLineNumber - 1 +
1413 (DiagLocLineNo - CppHashLocLineNo);
1414
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001415 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1416 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001417 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001418 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001419
Benjamin Kramer04a04262011-10-16 10:48:29 +00001420 if (Parser->SavedDiagHandler)
1421 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1422 else
1423 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001424}
1425
Rafael Espindola65366442011-06-05 02:43:45 +00001426bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1427 const std::vector<StringRef> &Parameters,
1428 const std::vector<std::vector<AsmToken> > &A,
1429 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001430 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001431 unsigned NParameters = Parameters.size();
1432 if (NParameters != 0 && NParameters != A.size())
1433 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001434
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001435 while (!Body.empty()) {
1436 // Scan for the next substitution.
1437 std::size_t End = Body.size(), Pos = 0;
1438 for (; Pos != End; ++Pos) {
1439 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001440 if (!NParameters) {
1441 // This macro has no parameters, look for $0, $1, etc.
1442 if (Body[Pos] != '$' || Pos + 1 == End)
1443 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001444
Rafael Espindola65366442011-06-05 02:43:45 +00001445 char Next = Body[Pos + 1];
1446 if (Next == '$' || Next == 'n' || isdigit(Next))
1447 break;
1448 } else {
1449 // This macro has parameters, look for \foo, \bar, etc.
1450 if (Body[Pos] == '\\' && Pos + 1 != End)
1451 break;
1452 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001453 }
1454
1455 // Add the prefix.
1456 OS << Body.slice(0, Pos);
1457
1458 // Check if we reached the end.
1459 if (Pos == End)
1460 break;
1461
Rafael Espindola65366442011-06-05 02:43:45 +00001462 if (!NParameters) {
1463 switch (Body[Pos+1]) {
1464 // $$ => $
1465 case '$':
1466 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001467 break;
1468
Rafael Espindola65366442011-06-05 02:43:45 +00001469 // $n => number of arguments
1470 case 'n':
1471 OS << A.size();
1472 break;
1473
1474 // $[0-9] => argument
1475 default: {
1476 // Missing arguments are ignored.
1477 unsigned Index = Body[Pos+1] - '0';
1478 if (Index >= A.size())
1479 break;
1480
1481 // Otherwise substitute with the token values, with spaces eliminated.
1482 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1483 ie = A[Index].end(); it != ie; ++it)
1484 OS << it->getString();
1485 break;
1486 }
1487 }
1488 Pos += 2;
1489 } else {
1490 unsigned I = Pos + 1;
1491 while (isalnum(Body[I]) && I + 1 != End)
1492 ++I;
1493
1494 const char *Begin = Body.data() + Pos +1;
1495 StringRef Argument(Begin, I - (Pos +1));
1496 unsigned Index = 0;
1497 for (; Index < NParameters; ++Index)
1498 if (Parameters[Index] == Argument)
1499 break;
1500
1501 // FIXME: We should error at the macro definition.
1502 if (Index == NParameters)
1503 return Error(L, "Parameter not found");
1504
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001505 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1506 ie = A[Index].end(); it != ie; ++it)
1507 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001508
Rafael Espindola65366442011-06-05 02:43:45 +00001509 Pos += 1 + Argument.size();
1510 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001511 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001512 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001513 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001514
1515 // We include the .endmacro in the buffer as our queue to exit the macro
1516 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001517 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001518 return false;
1519}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001520
Rafael Espindola65366442011-06-05 02:43:45 +00001521MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1522 MemoryBuffer *I)
1523 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1524{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001525}
1526
1527bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1528 const Macro *M) {
1529 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1530 // this, although we should protect against infinite loops.
1531 if (ActiveMacros.size() == 20)
1532 return TokError("macros cannot be nested more than 20 levels deep");
1533
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001534 // Parse the macro instantiation arguments.
1535 std::vector<std::vector<AsmToken> > MacroArguments;
1536 MacroArguments.push_back(std::vector<AsmToken>());
1537 unsigned ParenLevel = 0;
1538 for (;;) {
1539 if (Lexer.is(AsmToken::Eof))
1540 return TokError("unexpected token in macro instantiation");
1541 if (Lexer.is(AsmToken::EndOfStatement))
1542 break;
1543
1544 // If we aren't inside parentheses and this is a comma, start a new token
1545 // list.
1546 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1547 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001548 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001549 // Adjust the current parentheses level.
1550 if (Lexer.is(AsmToken::LParen))
1551 ++ParenLevel;
1552 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1553 --ParenLevel;
1554
1555 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001556 MacroArguments.back().push_back(getTok());
1557 }
1558 Lex();
1559 }
Jim Grosbach68f89a62012-04-16 21:18:49 +00001560 // If the last argument didn't end up with any tokens, it's not a real
1561 // argument and we should remove it from the list. This happens with either
1562 // a tailing comma or an empty argument list.
1563 if (MacroArguments.back().empty())
1564 MacroArguments.pop_back();
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001565
Rafael Espindola65366442011-06-05 02:43:45 +00001566 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1567 // to hold the macro body with substitutions.
1568 SmallString<256> Buf;
1569 StringRef Body = M->Body;
1570
1571 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1572 return true;
1573
1574 MemoryBuffer *Instantiation =
1575 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1576
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001577 // Create the macro instantiation object and add to the current macro
1578 // instantiation stack.
1579 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001580 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001581 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001582 ActiveMacros.push_back(MI);
1583
1584 // Jump to the macro instantiation and prime the lexer.
1585 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1586 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1587 Lex();
1588
1589 return false;
1590}
1591
1592void AsmParser::HandleMacroExit() {
1593 // Jump to the EndOfStatement we should return to, and consume it.
1594 JumpToLoc(ActiveMacros.back()->ExitLoc);
1595 Lex();
1596
1597 // Pop the instantiation entry.
1598 delete ActiveMacros.back();
1599 ActiveMacros.pop_back();
1600}
1601
Rafael Espindolae71cc862012-01-28 05:57:00 +00001602static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001603 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001604 case MCExpr::Binary: {
1605 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1606 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001607 break;
1608 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001609 case MCExpr::Target:
1610 case MCExpr::Constant:
1611 return false;
1612 case MCExpr::SymbolRef: {
1613 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001614 if (S.isVariable())
1615 return IsUsedIn(Sym, S.getVariableValue());
1616 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001617 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001618 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001619 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001620 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001621
1622 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001623}
1624
Nico Weber4c4c7322011-01-28 03:04:41 +00001625bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001626 // FIXME: Use better location, we should use proper tokens.
1627 SMLoc EqualLoc = Lexer.getLoc();
1628
Daniel Dunbar821e3332009-08-31 08:09:28 +00001629 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001630 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001631 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001632
Rafael Espindolae71cc862012-01-28 05:57:00 +00001633 // Note: we don't count b as used in "a = b". This is to allow
1634 // a = b
1635 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001636
Daniel Dunbar3f872332009-07-28 16:08:33 +00001637 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001638 return TokError("unexpected token in assignment");
1639
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001640 // Error on assignment to '.'.
1641 if (Name == ".") {
1642 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1643 "(use '.space' or '.org').)"));
1644 }
1645
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001646 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001647 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001648
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001649 // Validate that the LHS is allowed to be a variable (either it has not been
1650 // used as a symbol, or it is an absolute symbol).
1651 MCSymbol *Sym = getContext().LookupSymbol(Name);
1652 if (Sym) {
1653 // Diagnose assignment to a label.
1654 //
1655 // FIXME: Diagnostics. Note the location of the definition as a label.
1656 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001657 if (IsUsedIn(Sym, Value))
1658 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1659 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001660 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001661 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1662 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001663 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001664 return Error(EqualLoc, "redefinition of '" + Name + "'");
1665 else if (!Sym->isVariable())
1666 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001667 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001668 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1669 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001670
1671 // Don't count these checks as uses.
1672 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001673 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001674 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001675
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001676 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001677
1678 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001679 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001680
1681 return false;
1682}
1683
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001684/// ParseIdentifier:
1685/// ::= identifier
1686/// ::= string
1687bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001688 // The assembler has relaxed rules for accepting identifiers, in particular we
1689 // allow things like '.globl $foo', which would normally be separate
1690 // tokens. At this level, we have already lexed so we cannot (currently)
1691 // handle this as a context dependent token, instead we detect adjacent tokens
1692 // and return the combined identifier.
1693 if (Lexer.is(AsmToken::Dollar)) {
1694 SMLoc DollarLoc = getLexer().getLoc();
1695
1696 // Consume the dollar sign, and check for a following identifier.
1697 Lex();
1698 if (Lexer.isNot(AsmToken::Identifier))
1699 return true;
1700
1701 // We have a '$' followed by an identifier, make sure they are adjacent.
1702 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1703 return true;
1704
1705 // Construct the joined identifier and consume the token.
1706 Res = StringRef(DollarLoc.getPointer(),
1707 getTok().getIdentifier().size() + 1);
1708 Lex();
1709 return false;
1710 }
1711
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001712 if (Lexer.isNot(AsmToken::Identifier) &&
1713 Lexer.isNot(AsmToken::String))
1714 return true;
1715
Sean Callanan18b83232010-01-19 21:44:56 +00001716 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001717
Sean Callanan79ed1a82010-01-19 20:22:31 +00001718 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001719
1720 return false;
1721}
1722
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001723/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001724/// ::= .equ identifier ',' expression
1725/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001726/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001727bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001728 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001729
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001730 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001731 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001732
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001733 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001734 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001735 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001736
Nico Weber4c4c7322011-01-28 03:04:41 +00001737 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001738}
1739
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001740bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001741 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001742
1743 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001744 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001745 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1746 if (Str[i] != '\\') {
1747 Data += Str[i];
1748 continue;
1749 }
1750
1751 // Recognize escaped characters. Note that this escape semantics currently
1752 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1753 ++i;
1754 if (i == e)
1755 return TokError("unexpected backslash at end of string");
1756
1757 // Recognize octal sequences.
1758 if ((unsigned) (Str[i] - '0') <= 7) {
1759 // Consume up to three octal characters.
1760 unsigned Value = Str[i] - '0';
1761
1762 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1763 ++i;
1764 Value = Value * 8 + (Str[i] - '0');
1765
1766 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1767 ++i;
1768 Value = Value * 8 + (Str[i] - '0');
1769 }
1770 }
1771
1772 if (Value > 255)
1773 return TokError("invalid octal escape sequence (out of range)");
1774
1775 Data += (unsigned char) Value;
1776 continue;
1777 }
1778
1779 // Otherwise recognize individual escapes.
1780 switch (Str[i]) {
1781 default:
1782 // Just reject invalid escape sequences for now.
1783 return TokError("invalid escape sequence (unrecognized character)");
1784
1785 case 'b': Data += '\b'; break;
1786 case 'f': Data += '\f'; break;
1787 case 'n': Data += '\n'; break;
1788 case 'r': Data += '\r'; break;
1789 case 't': Data += '\t'; break;
1790 case '"': Data += '"'; break;
1791 case '\\': Data += '\\'; break;
1792 }
1793 }
1794
1795 return false;
1796}
1797
Daniel Dunbara0d14262009-06-24 23:30:00 +00001798/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001799/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1800bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001801 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001802 CheckForValidSection();
1803
Daniel Dunbara0d14262009-06-24 23:30:00 +00001804 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001805 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001806 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001807
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001808 std::string Data;
1809 if (ParseEscapedString(Data))
1810 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001811
1812 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001813 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001814 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1815
Sean Callanan79ed1a82010-01-19 20:22:31 +00001816 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001817
1818 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001819 break;
1820
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001821 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001822 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001823 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001824 }
1825 }
1826
Sean Callanan79ed1a82010-01-19 20:22:31 +00001827 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001828 return false;
1829}
1830
1831/// ParseDirectiveValue
1832/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1833bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001834 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001835 CheckForValidSection();
1836
Daniel Dunbara0d14262009-06-24 23:30:00 +00001837 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001838 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001839 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001840 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001841 return true;
1842
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001843 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001844 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1845 assert(Size <= 8 && "Invalid size");
1846 uint64_t IntValue = MCE->getValue();
1847 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1848 return Error(ExprLoc, "literal value out of range for directive");
1849 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1850 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001851 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001852
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001853 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001854 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001855
Daniel Dunbara0d14262009-06-24 23:30:00 +00001856 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001857 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001858 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001859 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001860 }
1861 }
1862
Sean Callanan79ed1a82010-01-19 20:22:31 +00001863 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001864 return false;
1865}
1866
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001867/// ParseDirectiveRealValue
1868/// ::= (.single | .double) [ expression (, expression)* ]
1869bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1870 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1871 CheckForValidSection();
1872
1873 for (;;) {
1874 // We don't truly support arithmetic on floating point expressions, so we
1875 // have to manually parse unary prefixes.
1876 bool IsNeg = false;
1877 if (getLexer().is(AsmToken::Minus)) {
1878 Lex();
1879 IsNeg = true;
1880 } else if (getLexer().is(AsmToken::Plus))
1881 Lex();
1882
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001883 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001884 getLexer().isNot(AsmToken::Real) &&
1885 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001886 return TokError("unexpected token in directive");
1887
1888 // Convert to an APFloat.
1889 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001890 StringRef IDVal = getTok().getString();
1891 if (getLexer().is(AsmToken::Identifier)) {
1892 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1893 Value = APFloat::getInf(Semantics);
1894 else if (!IDVal.compare_lower("nan"))
1895 Value = APFloat::getNaN(Semantics, false, ~0);
1896 else
1897 return TokError("invalid floating point literal");
1898 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001899 APFloat::opInvalidOp)
1900 return TokError("invalid floating point literal");
1901 if (IsNeg)
1902 Value.changeSign();
1903
1904 // Consume the numeric token.
1905 Lex();
1906
1907 // Emit the value as an integer.
1908 APInt AsInt = Value.bitcastToAPInt();
1909 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1910 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1911
1912 if (getLexer().is(AsmToken::EndOfStatement))
1913 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001914
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001915 if (getLexer().isNot(AsmToken::Comma))
1916 return TokError("unexpected token in directive");
1917 Lex();
1918 }
1919 }
1920
1921 Lex();
1922 return false;
1923}
1924
Daniel Dunbara0d14262009-06-24 23:30:00 +00001925/// ParseDirectiveSpace
1926/// ::= .space expression [ , expression ]
1927bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001928 CheckForValidSection();
1929
Daniel Dunbara0d14262009-06-24 23:30:00 +00001930 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001931 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001932 return true;
1933
1934 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001935 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1936 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001937 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001938 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001939
Daniel Dunbar475839e2009-06-29 20:37:27 +00001940 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001941 return true;
1942
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001943 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001944 return TokError("unexpected token in '.space' directive");
1945 }
1946
Sean Callanan79ed1a82010-01-19 20:22:31 +00001947 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001948
1949 if (NumBytes <= 0)
1950 return TokError("invalid number of bytes in '.space' directive");
1951
1952 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001953 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001954
1955 return false;
1956}
1957
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001958/// ParseDirectiveZero
1959/// ::= .zero expression
1960bool AsmParser::ParseDirectiveZero() {
1961 CheckForValidSection();
1962
1963 int64_t NumBytes;
1964 if (ParseAbsoluteExpression(NumBytes))
1965 return true;
1966
Rafael Espindolae452b172010-10-05 19:42:57 +00001967 int64_t Val = 0;
1968 if (getLexer().is(AsmToken::Comma)) {
1969 Lex();
1970 if (ParseAbsoluteExpression(Val))
1971 return true;
1972 }
1973
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001974 if (getLexer().isNot(AsmToken::EndOfStatement))
1975 return TokError("unexpected token in '.zero' directive");
1976
1977 Lex();
1978
Rafael Espindolae452b172010-10-05 19:42:57 +00001979 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001980
1981 return false;
1982}
1983
Daniel Dunbara0d14262009-06-24 23:30:00 +00001984/// ParseDirectiveFill
1985/// ::= .fill expression , expression , expression
1986bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001987 CheckForValidSection();
1988
Daniel Dunbara0d14262009-06-24 23:30:00 +00001989 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001990 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001991 return true;
1992
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001993 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001994 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001995 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001996
Daniel Dunbara0d14262009-06-24 23:30:00 +00001997 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001998 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001999 return true;
2000
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002001 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002002 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002003 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002004
Daniel Dunbara0d14262009-06-24 23:30:00 +00002005 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002006 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002007 return true;
2008
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002009 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002010 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002011
Sean Callanan79ed1a82010-01-19 20:22:31 +00002012 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002013
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002014 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2015 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002016
2017 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002018 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002019
2020 return false;
2021}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002022
2023/// ParseDirectiveOrg
2024/// ::= .org expression [ , expression ]
2025bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002026 CheckForValidSection();
2027
Daniel Dunbar821e3332009-08-31 08:09:28 +00002028 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002029 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002030 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002031 return true;
2032
2033 // Parse optional fill expression.
2034 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002035 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2036 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002037 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002038 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002039
Daniel Dunbar475839e2009-06-29 20:37:27 +00002040 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002041 return true;
2042
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002043 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002044 return TokError("unexpected token in '.org' directive");
2045 }
2046
Sean Callanan79ed1a82010-01-19 20:22:31 +00002047 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002048
Jim Grosbachebd4c052012-01-27 00:37:08 +00002049 // Only limited forms of relocatable expressions are accepted here, it
2050 // has to be relative to the current section. The streamer will return
2051 // 'true' if the expression wasn't evaluatable.
2052 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2053 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002054
2055 return false;
2056}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002057
2058/// ParseDirectiveAlign
2059/// ::= {.align, ...} expression [ , expression [ , expression ]]
2060bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002061 CheckForValidSection();
2062
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002063 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002064 int64_t Alignment;
2065 if (ParseAbsoluteExpression(Alignment))
2066 return true;
2067
2068 SMLoc MaxBytesLoc;
2069 bool HasFillExpr = false;
2070 int64_t FillExpr = 0;
2071 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002072 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2073 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002074 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002075 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002076
2077 // The fill expression can be omitted while specifying a maximum number of
2078 // alignment bytes, e.g:
2079 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002080 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002081 HasFillExpr = true;
2082 if (ParseAbsoluteExpression(FillExpr))
2083 return true;
2084 }
2085
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002086 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2087 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002088 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002089 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002090
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002091 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002092 if (ParseAbsoluteExpression(MaxBytesToFill))
2093 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002094
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002095 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002096 return TokError("unexpected token in directive");
2097 }
2098 }
2099
Sean Callanan79ed1a82010-01-19 20:22:31 +00002100 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002101
Daniel Dunbar648ac512010-05-17 21:54:30 +00002102 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002103 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002104
2105 // Compute alignment in bytes.
2106 if (IsPow2) {
2107 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002108 if (Alignment >= 32) {
2109 Error(AlignmentLoc, "invalid alignment value");
2110 Alignment = 31;
2111 }
2112
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002113 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002114 }
2115
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002116 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002117 if (MaxBytesLoc.isValid()) {
2118 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002119 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2120 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002121 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002122 }
2123
2124 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002125 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2126 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002127 MaxBytesToFill = 0;
2128 }
2129 }
2130
Daniel Dunbar648ac512010-05-17 21:54:30 +00002131 // Check whether we should use optimal code alignment for this .align
2132 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002133 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002134 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2135 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002136 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002137 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002138 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002139 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2140 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002141 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002142
2143 return false;
2144}
2145
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002146/// ParseDirectiveSymbolAttribute
2147/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002148bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002149 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002150 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002151 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002152 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002153
2154 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002155 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002156
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002157 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002158
Jim Grosbach10ec6502011-09-15 17:56:49 +00002159 // Assembler local symbols don't make any sense here. Complain loudly.
2160 if (Sym->isTemporary())
2161 return Error(Loc, "non-local symbol required in directive");
2162
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002163 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002164
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002165 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002166 break;
2167
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002168 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002169 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002170 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002171 }
2172 }
2173
Sean Callanan79ed1a82010-01-19 20:22:31 +00002174 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002175 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002176}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002177
2178/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002179/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2180bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002181 CheckForValidSection();
2182
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002183 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002184 StringRef Name;
2185 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002186 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002187
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002188 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002189 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002190
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002191 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002192 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002193 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002194
2195 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002196 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002197 if (ParseAbsoluteExpression(Size))
2198 return true;
2199
2200 int64_t Pow2Alignment = 0;
2201 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002202 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002203 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002204 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002205 if (ParseAbsoluteExpression(Pow2Alignment))
2206 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002207
Chris Lattner258281d2010-01-19 06:22:22 +00002208 // If this target takes alignments in bytes (not log) validate and convert.
2209 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2210 if (!isPowerOf2_64(Pow2Alignment))
2211 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2212 Pow2Alignment = Log2_64(Pow2Alignment);
2213 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002214 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002215
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002216 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002217 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002218
Sean Callanan79ed1a82010-01-19 20:22:31 +00002219 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002220
Chris Lattner1fc3d752009-07-09 17:25:12 +00002221 // NOTE: a size of zero for a .comm should create a undefined symbol
2222 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002223 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002224 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2225 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002226
Eric Christopherc260a3e2010-05-14 01:38:54 +00002227 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002228 // may internally end up wanting an alignment in bytes.
2229 // FIXME: Diagnose overflow.
2230 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002231 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2232 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002233
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002234 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002235 return Error(IDLoc, "invalid symbol redefinition");
2236
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002237 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002238 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002239 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002240 getStreamer().EmitZerofill(Ctx.getMachOSection(
2241 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2242 0, SectionKind::getBSS()),
2243 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002244 return false;
2245 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002246
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002247 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002248 return false;
2249}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002250
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002251/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002252/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002253bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002254 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002255 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002256
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002257 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002258 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002259 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002260
Sean Callanan79ed1a82010-01-19 20:22:31 +00002261 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002262
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002263 if (Str.empty())
2264 Error(Loc, ".abort detected. Assembly stopping.");
2265 else
2266 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002267 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002268
2269 return false;
2270}
Kevin Enderby71148242009-07-14 21:35:03 +00002271
Kevin Enderby1f049b22009-07-14 23:21:55 +00002272/// ParseDirectiveInclude
2273/// ::= .include "filename"
2274bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002275 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002276 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002277
Sean Callanan18b83232010-01-19 21:44:56 +00002278 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002279 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002280 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002281
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002282 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002283 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002284
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002285 // Strip the quotes.
2286 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002287
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002288 // Attempt to switch the lexer to the included file before consuming the end
2289 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002290 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002291 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002292 return true;
2293 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002294
2295 return false;
2296}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002297
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002298/// ParseDirectiveIncbin
2299/// ::= .incbin "filename"
2300bool AsmParser::ParseDirectiveIncbin() {
2301 if (getLexer().isNot(AsmToken::String))
2302 return TokError("expected string in '.incbin' directive");
2303
2304 std::string Filename = getTok().getString();
2305 SMLoc IncbinLoc = getLexer().getLoc();
2306 Lex();
2307
2308 if (getLexer().isNot(AsmToken::EndOfStatement))
2309 return TokError("unexpected token in '.incbin' directive");
2310
2311 // Strip the quotes.
2312 Filename = Filename.substr(1, Filename.size()-2);
2313
2314 // Attempt to process the included file.
2315 if (ProcessIncbinFile(Filename)) {
2316 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2317 return true;
2318 }
2319
2320 return false;
2321}
2322
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002323/// ParseDirectiveIf
2324/// ::= .if expression
2325bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002326 TheCondStack.push_back(TheCondState);
2327 TheCondState.TheCond = AsmCond::IfCond;
2328 if(TheCondState.Ignore) {
2329 EatToEndOfStatement();
2330 }
2331 else {
2332 int64_t ExprValue;
2333 if (ParseAbsoluteExpression(ExprValue))
2334 return true;
2335
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002336 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002337 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002338
Sean Callanan79ed1a82010-01-19 20:22:31 +00002339 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002340
2341 TheCondState.CondMet = ExprValue;
2342 TheCondState.Ignore = !TheCondState.CondMet;
2343 }
2344
2345 return false;
2346}
2347
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002348/// ParseDirectiveIfb
2349/// ::= .ifb string
2350bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2351 TheCondStack.push_back(TheCondState);
2352 TheCondState.TheCond = AsmCond::IfCond;
2353
2354 if(TheCondState.Ignore) {
2355 EatToEndOfStatement();
2356 } else {
2357 StringRef Str = ParseStringToEndOfStatement();
2358
2359 if (getLexer().isNot(AsmToken::EndOfStatement))
2360 return TokError("unexpected token in '.ifb' directive");
2361
2362 Lex();
2363
2364 TheCondState.CondMet = ExpectBlank == Str.empty();
2365 TheCondState.Ignore = !TheCondState.CondMet;
2366 }
2367
2368 return false;
2369}
2370
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002371/// ParseDirectiveIfc
2372/// ::= .ifc string1, string2
2373bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2374 TheCondStack.push_back(TheCondState);
2375 TheCondState.TheCond = AsmCond::IfCond;
2376
2377 if(TheCondState.Ignore) {
2378 EatToEndOfStatement();
2379 } else {
2380 StringRef Str1 = ParseStringToComma();
2381
2382 if (getLexer().isNot(AsmToken::Comma))
2383 return TokError("unexpected token in '.ifc' directive");
2384
2385 Lex();
2386
2387 StringRef Str2 = ParseStringToEndOfStatement();
2388
2389 if (getLexer().isNot(AsmToken::EndOfStatement))
2390 return TokError("unexpected token in '.ifc' directive");
2391
2392 Lex();
2393
2394 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2395 TheCondState.Ignore = !TheCondState.CondMet;
2396 }
2397
2398 return false;
2399}
2400
2401/// ParseDirectiveIfdef
2402/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002403bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2404 StringRef Name;
2405 TheCondStack.push_back(TheCondState);
2406 TheCondState.TheCond = AsmCond::IfCond;
2407
2408 if (TheCondState.Ignore) {
2409 EatToEndOfStatement();
2410 } else {
2411 if (ParseIdentifier(Name))
2412 return TokError("expected identifier after '.ifdef'");
2413
2414 Lex();
2415
2416 MCSymbol *Sym = getContext().LookupSymbol(Name);
2417
2418 if (expect_defined)
2419 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2420 else
2421 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2422 TheCondState.Ignore = !TheCondState.CondMet;
2423 }
2424
2425 return false;
2426}
2427
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002428/// ParseDirectiveElseIf
2429/// ::= .elseif expression
2430bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2431 if (TheCondState.TheCond != AsmCond::IfCond &&
2432 TheCondState.TheCond != AsmCond::ElseIfCond)
2433 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2434 " an .elseif");
2435 TheCondState.TheCond = AsmCond::ElseIfCond;
2436
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002437 bool LastIgnoreState = false;
2438 if (!TheCondStack.empty())
2439 LastIgnoreState = TheCondStack.back().Ignore;
2440 if (LastIgnoreState || TheCondState.CondMet) {
2441 TheCondState.Ignore = true;
2442 EatToEndOfStatement();
2443 }
2444 else {
2445 int64_t ExprValue;
2446 if (ParseAbsoluteExpression(ExprValue))
2447 return true;
2448
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002449 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002450 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002451
Sean Callanan79ed1a82010-01-19 20:22:31 +00002452 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002453 TheCondState.CondMet = ExprValue;
2454 TheCondState.Ignore = !TheCondState.CondMet;
2455 }
2456
2457 return false;
2458}
2459
2460/// ParseDirectiveElse
2461/// ::= .else
2462bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002463 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002464 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002465
Sean Callanan79ed1a82010-01-19 20:22:31 +00002466 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002467
2468 if (TheCondState.TheCond != AsmCond::IfCond &&
2469 TheCondState.TheCond != AsmCond::ElseIfCond)
2470 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2471 ".elseif");
2472 TheCondState.TheCond = AsmCond::ElseCond;
2473 bool LastIgnoreState = false;
2474 if (!TheCondStack.empty())
2475 LastIgnoreState = TheCondStack.back().Ignore;
2476 if (LastIgnoreState || TheCondState.CondMet)
2477 TheCondState.Ignore = true;
2478 else
2479 TheCondState.Ignore = false;
2480
2481 return false;
2482}
2483
2484/// ParseDirectiveEndIf
2485/// ::= .endif
2486bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002487 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002488 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002489
Sean Callanan79ed1a82010-01-19 20:22:31 +00002490 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002491
2492 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2493 TheCondStack.empty())
2494 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2495 ".else");
2496 if (!TheCondStack.empty()) {
2497 TheCondState = TheCondStack.back();
2498 TheCondStack.pop_back();
2499 }
2500
2501 return false;
2502}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002503
2504/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002505/// ::= .file [number] filename
2506/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002507bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002508 // FIXME: I'm not sure what this is.
2509 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002510 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002511 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002512 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002513 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002514
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002515 if (FileNumber < 1)
2516 return TokError("file number less than one");
2517 }
2518
Daniel Dunbareceec052010-07-12 17:45:27 +00002519 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002520 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002521
Nick Lewycky44d798d2011-10-17 23:05:28 +00002522 // Usually the directory and filename together, otherwise just the directory.
2523 StringRef Path = getTok().getString();
2524 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002525 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002526
Nick Lewycky44d798d2011-10-17 23:05:28 +00002527 StringRef Directory;
2528 StringRef Filename;
2529 if (getLexer().is(AsmToken::String)) {
2530 if (FileNumber == -1)
2531 return TokError("explicit path specified, but no file number");
2532 Filename = getTok().getString();
2533 Filename = Filename.substr(1, Filename.size()-2);
2534 Directory = Path;
2535 Lex();
2536 } else {
2537 Filename = Path;
2538 }
2539
Daniel Dunbareceec052010-07-12 17:45:27 +00002540 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002541 return TokError("unexpected token in '.file' directive");
2542
Chris Lattnerd32e8032010-01-25 19:02:58 +00002543 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002544 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002545 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002546 if (getContext().getGenDwarfForAssembly() == true)
2547 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2548 "used to generate dwarf debug info for assembly code");
2549
Nick Lewycky44d798d2011-10-17 23:05:28 +00002550 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002551 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002552 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002553
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002554 return false;
2555}
2556
2557/// ParseDirectiveLine
2558/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002559bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002560 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2561 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002562 return TokError("unexpected token in '.line' directive");
2563
Sean Callanan18b83232010-01-19 21:44:56 +00002564 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002565 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002566 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002567
2568 // FIXME: Do something with the .line.
2569 }
2570
Daniel Dunbareceec052010-07-12 17:45:27 +00002571 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002572 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002573
2574 return false;
2575}
2576
2577
2578/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002579/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002580/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2581/// The first number is a file number, must have been previously assigned with
2582/// a .file directive, the second number is the line number and optionally the
2583/// third number is a column position (zero if not specified). The remaining
2584/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002585bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002586
Daniel Dunbareceec052010-07-12 17:45:27 +00002587 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002588 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002589 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002590 if (FileNumber < 1)
2591 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002592 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002593 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002594 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002595
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002596 int64_t LineNumber = 0;
2597 if (getLexer().is(AsmToken::Integer)) {
2598 LineNumber = getTok().getIntVal();
2599 if (LineNumber < 1)
2600 return TokError("line number less than one in '.loc' directive");
2601 Lex();
2602 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002603
2604 int64_t ColumnPos = 0;
2605 if (getLexer().is(AsmToken::Integer)) {
2606 ColumnPos = getTok().getIntVal();
2607 if (ColumnPos < 0)
2608 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002609 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002610 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002611
Kevin Enderbyc0957932010-09-30 16:52:03 +00002612 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002613 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002614 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002615 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2616 for (;;) {
2617 if (getLexer().is(AsmToken::EndOfStatement))
2618 break;
2619
2620 StringRef Name;
2621 SMLoc Loc = getTok().getLoc();
2622 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002623 return TokError("unexpected token in '.loc' directive");
2624
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002625 if (Name == "basic_block")
2626 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2627 else if (Name == "prologue_end")
2628 Flags |= DWARF2_FLAG_PROLOGUE_END;
2629 else if (Name == "epilogue_begin")
2630 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2631 else if (Name == "is_stmt") {
2632 SMLoc Loc = getTok().getLoc();
2633 const MCExpr *Value;
2634 if (getParser().ParseExpression(Value))
2635 return true;
2636 // The expression must be the constant 0 or 1.
2637 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2638 int Value = MCE->getValue();
2639 if (Value == 0)
2640 Flags &= ~DWARF2_FLAG_IS_STMT;
2641 else if (Value == 1)
2642 Flags |= DWARF2_FLAG_IS_STMT;
2643 else
2644 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002645 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002646 else {
2647 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2648 }
2649 }
2650 else if (Name == "isa") {
2651 SMLoc Loc = getTok().getLoc();
2652 const MCExpr *Value;
2653 if (getParser().ParseExpression(Value))
2654 return true;
2655 // The expression must be a constant greater or equal to 0.
2656 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2657 int Value = MCE->getValue();
2658 if (Value < 0)
2659 return Error(Loc, "isa number less than zero");
2660 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002661 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002662 else {
2663 return Error(Loc, "isa number not a constant value");
2664 }
2665 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002666 else if (Name == "discriminator") {
2667 if (getParser().ParseAbsoluteExpression(Discriminator))
2668 return true;
2669 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002670 else {
2671 return Error(Loc, "unknown sub-directive in '.loc' directive");
2672 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002673
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002674 if (getLexer().is(AsmToken::EndOfStatement))
2675 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002676 }
2677 }
2678
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002679 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002680 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002681
2682 return false;
2683}
2684
Daniel Dunbar138abae2010-10-16 04:56:42 +00002685/// ParseDirectiveStabs
2686/// ::= .stabs string, number, number, number
2687bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2688 SMLoc DirectiveLoc) {
2689 return TokError("unsupported directive '" + Directive + "'");
2690}
2691
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002692/// ParseDirectiveCFISections
2693/// ::= .cfi_sections section [, section]
2694bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2695 SMLoc DirectiveLoc) {
2696 StringRef Name;
2697 bool EH = false;
2698 bool Debug = false;
2699
2700 if (getParser().ParseIdentifier(Name))
2701 return TokError("Expected an identifier");
2702
2703 if (Name == ".eh_frame")
2704 EH = true;
2705 else if (Name == ".debug_frame")
2706 Debug = true;
2707
2708 if (getLexer().is(AsmToken::Comma)) {
2709 Lex();
2710
2711 if (getParser().ParseIdentifier(Name))
2712 return TokError("Expected an identifier");
2713
2714 if (Name == ".eh_frame")
2715 EH = true;
2716 else if (Name == ".debug_frame")
2717 Debug = true;
2718 }
2719
2720 getStreamer().EmitCFISections(EH, Debug);
2721
2722 return false;
2723}
2724
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002725/// ParseDirectiveCFIStartProc
2726/// ::= .cfi_startproc
2727bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2728 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002729 getStreamer().EmitCFIStartProc();
2730 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002731}
2732
2733/// ParseDirectiveCFIEndProc
2734/// ::= .cfi_endproc
2735bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002736 getStreamer().EmitCFIEndProc();
2737 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002738}
2739
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002740/// ParseRegisterOrRegisterNumber - parse register name or number.
2741bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2742 SMLoc DirectiveLoc) {
2743 unsigned RegNo;
2744
Jim Grosbach6f888a82011-06-02 17:14:04 +00002745 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002746 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2747 DirectiveLoc))
2748 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002749 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002750 } else
2751 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002752
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002753 return false;
2754}
2755
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002756/// ParseDirectiveCFIDefCfa
2757/// ::= .cfi_def_cfa register, offset
2758bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2759 SMLoc DirectiveLoc) {
2760 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002761 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002762 return true;
2763
2764 if (getLexer().isNot(AsmToken::Comma))
2765 return TokError("unexpected token in directive");
2766 Lex();
2767
2768 int64_t Offset = 0;
2769 if (getParser().ParseAbsoluteExpression(Offset))
2770 return true;
2771
Rafael Espindola066c2f42011-04-12 23:59:07 +00002772 getStreamer().EmitCFIDefCfa(Register, Offset);
2773 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002774}
2775
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002776/// ParseDirectiveCFIDefCfaOffset
2777/// ::= .cfi_def_cfa_offset offset
2778bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2779 SMLoc DirectiveLoc) {
2780 int64_t Offset = 0;
2781 if (getParser().ParseAbsoluteExpression(Offset))
2782 return true;
2783
Rafael Espindola066c2f42011-04-12 23:59:07 +00002784 getStreamer().EmitCFIDefCfaOffset(Offset);
2785 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002786}
2787
2788/// ParseDirectiveCFIAdjustCfaOffset
2789/// ::= .cfi_adjust_cfa_offset adjustment
2790bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2791 SMLoc DirectiveLoc) {
2792 int64_t Adjustment = 0;
2793 if (getParser().ParseAbsoluteExpression(Adjustment))
2794 return true;
2795
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002796 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2797 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002798}
2799
2800/// ParseDirectiveCFIDefCfaRegister
2801/// ::= .cfi_def_cfa_register register
2802bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2803 SMLoc DirectiveLoc) {
2804 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002805 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002806 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002807
Rafael Espindola066c2f42011-04-12 23:59:07 +00002808 getStreamer().EmitCFIDefCfaRegister(Register);
2809 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002810}
2811
2812/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002813/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002814bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2815 int64_t Register = 0;
2816 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002817
2818 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002819 return true;
2820
2821 if (getLexer().isNot(AsmToken::Comma))
2822 return TokError("unexpected token in directive");
2823 Lex();
2824
2825 if (getParser().ParseAbsoluteExpression(Offset))
2826 return true;
2827
Rafael Espindola066c2f42011-04-12 23:59:07 +00002828 getStreamer().EmitCFIOffset(Register, Offset);
2829 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002830}
2831
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002832/// ParseDirectiveCFIRelOffset
2833/// ::= .cfi_rel_offset register, offset
2834bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2835 SMLoc DirectiveLoc) {
2836 int64_t Register = 0;
2837
2838 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2839 return true;
2840
2841 if (getLexer().isNot(AsmToken::Comma))
2842 return TokError("unexpected token in directive");
2843 Lex();
2844
2845 int64_t Offset = 0;
2846 if (getParser().ParseAbsoluteExpression(Offset))
2847 return true;
2848
Rafael Espindola25f492e2011-04-12 16:12:03 +00002849 getStreamer().EmitCFIRelOffset(Register, Offset);
2850 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002851}
2852
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002853static bool isValidEncoding(int64_t Encoding) {
2854 if (Encoding & ~0xff)
2855 return false;
2856
2857 if (Encoding == dwarf::DW_EH_PE_omit)
2858 return true;
2859
2860 const unsigned Format = Encoding & 0xf;
2861 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2862 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2863 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2864 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2865 return false;
2866
Rafael Espindolacaf11582010-12-29 04:31:26 +00002867 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002868 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002869 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002870 return false;
2871
2872 return true;
2873}
2874
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002875/// ParseDirectiveCFIPersonalityOrLsda
2876/// ::= .cfi_personality encoding, [symbol_name]
2877/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002878bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002879 SMLoc DirectiveLoc) {
2880 int64_t Encoding = 0;
2881 if (getParser().ParseAbsoluteExpression(Encoding))
2882 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002883 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002884 return false;
2885
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002886 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002887 return TokError("unsupported encoding.");
2888
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002889 if (getLexer().isNot(AsmToken::Comma))
2890 return TokError("unexpected token in directive");
2891 Lex();
2892
2893 StringRef Name;
2894 if (getParser().ParseIdentifier(Name))
2895 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002896
2897 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2898
2899 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002900 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002901 else {
2902 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002903 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002904 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002905 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002906}
2907
Rafael Espindolafe024d02010-12-28 18:36:23 +00002908/// ParseDirectiveCFIRememberState
2909/// ::= .cfi_remember_state
2910bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2911 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002912 getStreamer().EmitCFIRememberState();
2913 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002914}
2915
2916/// ParseDirectiveCFIRestoreState
2917/// ::= .cfi_remember_state
2918bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2919 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002920 getStreamer().EmitCFIRestoreState();
2921 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002922}
2923
Rafael Espindolac5754392011-04-12 15:31:05 +00002924/// ParseDirectiveCFISameValue
2925/// ::= .cfi_same_value register
2926bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2927 SMLoc DirectiveLoc) {
2928 int64_t Register = 0;
2929
2930 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2931 return true;
2932
2933 getStreamer().EmitCFISameValue(Register);
2934
2935 return false;
2936}
2937
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00002938/// ParseDirectiveCFIRestore
2939/// ::= .cfi_restore register
2940bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
2941 SMLoc DirectiveLoc) {
2942 int64_t Register = 0;
2943 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2944 return true;
2945
2946 getStreamer().EmitCFIRestore(Register);
2947
2948 return false;
2949}
2950
Rafael Espindola6f0b1812011-12-29 20:24:47 +00002951/// ParseDirectiveCFIEscape
2952/// ::= .cfi_escape expression[,...]
2953bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
2954 SMLoc DirectiveLoc) {
2955 std::string Values;
2956 int64_t CurrValue;
2957 if (getParser().ParseAbsoluteExpression(CurrValue))
2958 return true;
2959
2960 Values.push_back((uint8_t)CurrValue);
2961
2962 while (getLexer().is(AsmToken::Comma)) {
2963 Lex();
2964
2965 if (getParser().ParseAbsoluteExpression(CurrValue))
2966 return true;
2967
2968 Values.push_back((uint8_t)CurrValue);
2969 }
2970
2971 getStreamer().EmitCFIEscape(Values);
2972 return false;
2973}
2974
Rafael Espindola16d7d432012-01-23 21:51:52 +00002975/// ParseDirectiveCFISignalFrame
2976/// ::= .cfi_signal_frame
2977bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
2978 SMLoc DirectiveLoc) {
2979 if (getLexer().isNot(AsmToken::EndOfStatement))
2980 return Error(getLexer().getLoc(),
2981 "unexpected token in '" + Directive + "' directive");
2982
2983 getStreamer().EmitCFISignalFrame();
2984
2985 return false;
2986}
2987
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002988/// ParseDirectiveMacrosOnOff
2989/// ::= .macros_on
2990/// ::= .macros_off
2991bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2992 SMLoc DirectiveLoc) {
2993 if (getLexer().isNot(AsmToken::EndOfStatement))
2994 return Error(getLexer().getLoc(),
2995 "unexpected token in '" + Directive + "' directive");
2996
2997 getParser().MacrosEnabled = Directive == ".macros_on";
2998
2999 return false;
3000}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003001
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003002/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003003/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003004bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3005 SMLoc DirectiveLoc) {
3006 StringRef Name;
3007 if (getParser().ParseIdentifier(Name))
3008 return TokError("expected identifier in directive");
3009
Rafael Espindola65366442011-06-05 02:43:45 +00003010 std::vector<StringRef> Parameters;
3011 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3012 for(;;) {
3013 StringRef Parameter;
3014 if (getParser().ParseIdentifier(Parameter))
3015 return TokError("expected identifier in directive");
3016 Parameters.push_back(Parameter);
3017
3018 if (getLexer().isNot(AsmToken::Comma))
3019 break;
3020 Lex();
3021 }
3022 }
3023
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003024 if (getLexer().isNot(AsmToken::EndOfStatement))
3025 return TokError("unexpected token in '.macro' directive");
3026
3027 // Eat the end of statement.
3028 Lex();
3029
3030 AsmToken EndToken, StartToken = getTok();
3031
3032 // Lex the macro definition.
3033 for (;;) {
3034 // Check whether we have reached the end of the file.
3035 if (getLexer().is(AsmToken::Eof))
3036 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3037
3038 // Otherwise, check whether we have reach the .endmacro.
3039 if (getLexer().is(AsmToken::Identifier) &&
3040 (getTok().getIdentifier() == ".endm" ||
3041 getTok().getIdentifier() == ".endmacro")) {
3042 EndToken = getTok();
3043 Lex();
3044 if (getLexer().isNot(AsmToken::EndOfStatement))
3045 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3046 "' directive");
3047 break;
3048 }
3049
3050 // Otherwise, scan til the end of the statement.
3051 getParser().EatToEndOfStatement();
3052 }
3053
3054 if (getParser().MacroMap.lookup(Name)) {
3055 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3056 }
3057
3058 const char *BodyStart = StartToken.getLoc().getPointer();
3059 const char *BodyEnd = EndToken.getLoc().getPointer();
3060 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003061 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003062 return false;
3063}
3064
3065/// ParseDirectiveEndMacro
3066/// ::= .endm
3067/// ::= .endmacro
3068bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
3069 SMLoc DirectiveLoc) {
3070 if (getLexer().isNot(AsmToken::EndOfStatement))
3071 return TokError("unexpected token in '" + Directive + "' directive");
3072
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003073 // If we are inside a macro instantiation, terminate the current
3074 // instantiation.
3075 if (!getParser().ActiveMacros.empty()) {
3076 getParser().HandleMacroExit();
3077 return false;
3078 }
3079
3080 // Otherwise, this .endmacro is a stray entry in the file; well formed
3081 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003082 return TokError("unexpected '" + Directive + "' in file, "
3083 "no current macro definition");
3084}
3085
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003086bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003087 getParser().CheckForValidSection();
3088
3089 const MCExpr *Value;
3090
3091 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003092 return true;
3093
3094 if (getLexer().isNot(AsmToken::EndOfStatement))
3095 return TokError("unexpected token in directive");
3096
3097 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003098 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003099 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003100 getStreamer().EmitULEB128Value(Value);
3101
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003102 return false;
3103}
3104
3105
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003106/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003107MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003108 MCContext &C, MCStreamer &Out,
3109 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003110 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003111}