blob: 990fd17f5987e7a349df880f803c42abaa236149 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000023#include "llvm/MC/MCParser/AsmCond.h"
24#include "llvm/MC/MCParser/AsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000027#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000028#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000029#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000030#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000031#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000032#include "llvm/Support/CommandLine.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000033#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000034#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000037#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000038#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000039using namespace llvm;
40
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000041static cl::opt<bool>
42FatalAssemblerWarnings("fatal-assembler-warnings",
43 cl::desc("Consider warnings as error"));
44
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000045namespace {
46
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000047/// \brief Helper class for tracking macro definitions.
48struct Macro {
49 StringRef Name;
50 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000051 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000052
53public:
Rafael Espindola65366442011-06-05 02:43:45 +000054 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
55 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000056};
57
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000058/// \brief Helper class for storing information about an active macro
59/// instantiation.
60struct MacroInstantiation {
61 /// The macro being instantiated.
62 const Macro *TheMacro;
63
64 /// The macro instantiation with substitutions.
65 MemoryBuffer *Instantiation;
66
67 /// The location of the instantiation.
68 SMLoc InstantiationLoc;
69
70 /// The location where parsing should resume upon instantiation completion.
71 SMLoc ExitLoc;
72
73public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000074 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000075 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000076};
77
Daniel Dunbaraef87e32010-07-18 18:31:38 +000078/// \brief The concrete assembly parser instance.
79class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000080 friend class GenericAsmParser;
81
Daniel Dunbaraef87e32010-07-18 18:31:38 +000082 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
83 void operator=(const AsmParser &); // DO NOT IMPLEMENT
84private:
85 AsmLexer Lexer;
86 MCContext &Ctx;
87 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000088 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000090 SourceMgr::DiagHandlerTy SavedDiagHandler;
91 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000092 MCAsmParserExtension *GenericParser;
93 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000094
Daniel Dunbaraef87e32010-07-18 18:31:38 +000095 /// This is the current buffer index we're lexing from as managed by the
96 /// SourceMgr object.
97 int CurBuffer;
98
99 AsmCond TheCondState;
100 std::vector<AsmCond> TheCondStack;
101
102 /// DirectiveMap - This is a table handlers for directives. Each handler is
103 /// invoked after the directive identifier is read and is responsible for
104 /// parsing and validating the rest of the directive. The handler is passed
105 /// in the directive name and the location of the directive keyword.
106 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000107
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000108 /// MacroMap - Map of currently defined macros.
109 StringMap<Macro*> MacroMap;
110
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000111 /// ActiveMacros - Stack of active macro instantiations.
112 std::vector<MacroInstantiation*> ActiveMacros;
113
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000114 /// Boolean tracking whether macro substitution is enabled.
115 unsigned MacrosEnabled : 1;
116
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000117 /// Flag tracking whether any errors have been encountered.
118 unsigned HadError : 1;
119
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000120 /// The values from the last parsed cpp hash file line comment if any.
121 StringRef CppHashFilename;
122 int64_t CppHashLineNumber;
123 SMLoc CppHashLoc;
124
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000125public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000126 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000127 const MCAsmInfo &MAI);
128 ~AsmParser();
129
130 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
131
132 void AddDirectiveHandler(MCAsmParserExtension *Object,
133 StringRef Directive,
134 DirectiveHandler Handler) {
135 DirectiveMap[Directive] = std::make_pair(Object, Handler);
136 }
137
138public:
139 /// @name MCAsmParser Interface
140 /// {
141
142 virtual SourceMgr &getSourceManager() { return SrcMgr; }
143 virtual MCAsmLexer &getLexer() { return Lexer; }
144 virtual MCContext &getContext() { return Ctx; }
145 virtual MCStreamer &getStreamer() { return Out; }
146
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000147 virtual bool Warning(SMLoc L, const Twine &Msg,
148 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
149 virtual bool Error(SMLoc L, const Twine &Msg,
150 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000151
152 const AsmToken &Lex();
153
154 bool ParseExpression(const MCExpr *&Res);
155 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
156 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
157 virtual bool ParseAbsoluteExpression(int64_t &Res);
158
159 /// }
160
161private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000162 void CheckForValidSection();
163
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000164 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000165 void EatToEndOfLine();
166 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000167
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000168 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000169 bool expandMacro(SmallString<256> &Buf, StringRef Body,
170 const std::vector<StringRef> &Parameters,
171 const std::vector<std::vector<AsmToken> > &A,
172 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000173 void HandleMacroExit();
174
175 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000176 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000177 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
178 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000179 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000180 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000181
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000182 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
183 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000184
185 /// \brief Reset the current lexer position to that given by \arg Loc. The
186 /// current token is not set; clients should ensure Lex() is called
187 /// subsequently.
188 void JumpToLoc(SMLoc Loc);
189
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000190 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000191
192 /// \brief Parse up to the end of statement and a return the contents from the
193 /// current token until the end of the statement; the current token on exit
194 /// will be either the EndOfStatement or EOF.
195 StringRef ParseStringToEndOfStatement();
196
Nico Weber4c4c7322011-01-28 03:04:41 +0000197 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000198
199 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
200 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
201 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000202 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000203
204 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
205 /// and set \arg Res to the identifier contents.
206 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000207
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000208 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000209
210 // ".ascii", ".asciiz", ".string"
211 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000212 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000213 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000214 bool ParseDirectiveFill(); // ".fill"
215 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000216 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000217 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000218 bool ParseDirectiveOrg(); // ".org"
219 // ".align{,32}", ".p2align{,w,l}"
220 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
221
222 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
223 /// accepts a single symbol (which should be a label or an external).
224 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000225
226 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
227
228 bool ParseDirectiveAbort(); // ".abort"
229 bool ParseDirectiveInclude(); // ".include"
230
231 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000232 // ".ifdef" or ".ifndef", depending on expect_defined
233 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000234 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
235 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
236 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
237
238 /// ParseEscapedString - Parse the current token as a string which may include
239 /// escaped characters and return the string contents.
240 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000241
242 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
243 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000244};
245
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000246/// \brief Generic implementations of directive handling, etc. which is shared
247/// (or the default, at least) for all assembler parser.
248class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000249 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
250 void AddDirectiveHandler(StringRef Directive) {
251 getParser().AddDirectiveHandler(this, Directive,
252 HandleDirective<GenericAsmParser, Handler>);
253 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000254public:
255 GenericAsmParser() {}
256
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000257 AsmParser &getParser() {
258 return (AsmParser&) this->MCAsmParserExtension::getParser();
259 }
260
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000261 virtual void Initialize(MCAsmParser &Parser) {
262 // Call the base implementation.
263 this->MCAsmParserExtension::Initialize(Parser);
264
265 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000269 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000270
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000271 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
273 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
275 ".cfi_startproc");
276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
277 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000278 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
279 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000280 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
281 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000282 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
283 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000284 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
285 ".cfi_def_cfa_register");
286 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
287 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000288 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
289 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000290 AddDirectiveHandler<
291 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
292 AddDirectiveHandler<
293 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000294 AddDirectiveHandler<
295 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
296 AddDirectiveHandler<
297 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000298 AddDirectiveHandler<
299 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000300
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000301 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000302 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
303 ".macros_on");
304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
305 ".macros_off");
306 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
307 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
308 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000309
310 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
311 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000312 }
313
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000314 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
315
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000316 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
317 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
318 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000319 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000320 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000321 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
322 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000323 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000324 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000325 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000326 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
327 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000328 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000329 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000330 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
331 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000332 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000333
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000334 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000335 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
336 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000337
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000338 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000339};
340
341}
342
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000343namespace llvm {
344
345extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000346extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000347extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000348
349}
350
Chris Lattneraaec2052010-01-19 19:46:13 +0000351enum { DEFAULT_ADDRSPACE = 0 };
352
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000353AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000354 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000355 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000356 GenericParser(new GenericAsmParser), PlatformParser(0),
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000357 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000358 // Save the old handler.
359 SavedDiagHandler = SrcMgr.getDiagHandler();
360 SavedDiagContext = SrcMgr.getDiagContext();
361 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000362 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000363 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000364
365 // Initialize the generic parser.
366 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000367
368 // Initialize the platform / file format parser.
369 //
370 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
371 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000372 if (_MAI.hasMicrosoftFastStdCallMangling()) {
373 PlatformParser = createCOFFAsmParser();
374 PlatformParser->Initialize(*this);
375 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000376 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000377 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000378 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000379 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000380 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000381 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000382}
383
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000384AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000385 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
386
387 // Destroy any macros.
388 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
389 ie = MacroMap.end(); it != ie; ++it)
390 delete it->getValue();
391
Daniel Dunbare4749702010-07-12 18:12:02 +0000392 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000393 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000394}
395
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000396void AsmParser::PrintMacroInstantiations() {
397 // Print the active macro instantiation stack.
398 for (std::vector<MacroInstantiation*>::const_reverse_iterator
399 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000400 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
401 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000402}
403
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000404bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000405 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000406 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000407 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000408 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000409 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000410}
411
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000412bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000413 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000414 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000415 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000416 return true;
417}
418
Sean Callananfd0b0282010-01-21 00:19:58 +0000419bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000420 std::string IncludedFile;
421 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000422 if (NewBuf == -1)
423 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000424
Sean Callananfd0b0282010-01-21 00:19:58 +0000425 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000426
Sean Callananfd0b0282010-01-21 00:19:58 +0000427 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000428
Sean Callananfd0b0282010-01-21 00:19:58 +0000429 return false;
430}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000431
432void AsmParser::JumpToLoc(SMLoc Loc) {
433 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
434 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
435}
436
Sean Callananfd0b0282010-01-21 00:19:58 +0000437const AsmToken &AsmParser::Lex() {
438 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000439
Sean Callananfd0b0282010-01-21 00:19:58 +0000440 if (tok->is(AsmToken::Eof)) {
441 // If this is the end of an included file, pop the parent file off the
442 // include stack.
443 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
444 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000445 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000446 tok = &Lexer.Lex();
447 }
448 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000449
Sean Callananfd0b0282010-01-21 00:19:58 +0000450 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000451 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000452
Sean Callananfd0b0282010-01-21 00:19:58 +0000453 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000454}
455
Chris Lattner79180e22010-04-05 23:15:42 +0000456bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000457 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000458 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000459 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000460
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000461 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000462 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000463
464 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000465 AsmCond StartingCondState = TheCondState;
466
Kevin Enderby613b7572011-11-01 22:27:22 +0000467 // If we are generating dwarf for assembly source files save the initial text
468 // section and generate a .file directive.
469 if (getContext().getGenDwarfForAssembly()) {
470 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
471 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
472 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
473 }
474
Chris Lattnerb717fb02009-07-02 21:53:43 +0000475 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000476 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000477 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000478
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000479 // We had an error, validate that one was emitted and recover by skipping to
480 // the next line.
481 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000482 EatToEndOfStatement();
483 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000484
485 if (TheCondState.TheCond != StartingCondState.TheCond ||
486 TheCondState.Ignore != StartingCondState.Ignore)
487 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000488
489 // Check to see there are no empty DwarfFile slots.
490 const std::vector<MCDwarfFile *> &MCDwarfFiles =
491 getContext().getMCDwarfFiles();
492 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000493 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000494 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000495 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000496
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000497 // Check to see that all assembler local symbols were actually defined.
498 // Targets that don't do subsections via symbols may not want this, though,
499 // so conservatively exclude them. Only do this if we're finalizing, though,
500 // as otherwise we won't necessarilly have seen everything yet.
501 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
502 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
503 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
504 e = Symbols.end();
505 i != e; ++i) {
506 MCSymbol *Sym = i->getValue();
507 // Variable symbols may not be marked as defined, so check those
508 // explicitly. If we know it's a variable, we have a definition for
509 // the purposes of this check.
510 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
511 // FIXME: We would really like to refer back to where the symbol was
512 // first referenced for a source location. We need to add something
513 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000514 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
515 "assembler local symbol '" + Sym->getName() +
516 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000517 }
518 }
519
520
Chris Lattner79180e22010-04-05 23:15:42 +0000521 // Finalize the output stream if there are no errors and if the client wants
522 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000523 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000524 Out.Finish();
525
Chris Lattnerb717fb02009-07-02 21:53:43 +0000526 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000527}
528
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000529void AsmParser::CheckForValidSection() {
530 if (!getStreamer().getCurrentSection()) {
531 TokError("expected section directive before assembly directive");
532 Out.SwitchSection(Ctx.getMachOSection(
533 "__TEXT", "__text",
534 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
535 0, SectionKind::getText()));
536 }
537}
538
Chris Lattner2cf5f142009-06-22 01:29:09 +0000539/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
540void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000541 while (Lexer.isNot(AsmToken::EndOfStatement) &&
542 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000543 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000544
Chris Lattner2cf5f142009-06-22 01:29:09 +0000545 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000546 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000547 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000548}
549
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000550StringRef AsmParser::ParseStringToEndOfStatement() {
551 const char *Start = getTok().getLoc().getPointer();
552
553 while (Lexer.isNot(AsmToken::EndOfStatement) &&
554 Lexer.isNot(AsmToken::Eof))
555 Lex();
556
557 const char *End = getTok().getLoc().getPointer();
558 return StringRef(Start, End - Start);
559}
Chris Lattnerc4193832009-06-22 05:51:26 +0000560
Chris Lattner74ec1a32009-06-22 06:32:03 +0000561/// ParseParenExpr - Parse a paren expression and return it.
562/// NOTE: This assumes the leading '(' has already been consumed.
563///
564/// parenexpr ::= expr)
565///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000566bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000567 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000568 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000569 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000570 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000571 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000572 return false;
573}
Chris Lattnerc4193832009-06-22 05:51:26 +0000574
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000575/// ParseBracketExpr - Parse a bracket expression and return it.
576/// NOTE: This assumes the leading '[' has already been consumed.
577///
578/// bracketexpr ::= expr]
579///
580bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
581 if (ParseExpression(Res)) return true;
582 if (Lexer.isNot(AsmToken::RBrac))
583 return TokError("expected ']' in brackets expression");
584 EndLoc = Lexer.getLoc();
585 Lex();
586 return false;
587}
588
Chris Lattner74ec1a32009-06-22 06:32:03 +0000589/// ParsePrimaryExpr - Parse a primary expression and return it.
590/// primaryexpr ::= (parenexpr
591/// primaryexpr ::= symbol
592/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000593/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000594/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000595bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000596 switch (Lexer.getKind()) {
597 default:
598 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000599 // If we have an error assume that we've already handled it.
600 case AsmToken::Error:
601 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000602 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000603 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000604 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000605 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000606 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000607 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000608 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000609 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000610 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000611 EndLoc = Lexer.getLoc();
612
613 StringRef Identifier;
614 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000615 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000616
Daniel Dunbarfffff912009-10-16 01:34:54 +0000617 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000618 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000619 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000620
621 // Lookup the symbol variant if used.
622 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000623 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000624 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000625 if (Variant == MCSymbolRefExpr::VK_Invalid) {
626 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000627 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000628 }
629 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000630
Daniel Dunbarfffff912009-10-16 01:34:54 +0000631 // If this is an absolute variable reference, substitute it now to preserve
632 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000633 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000634 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000635 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000636
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000637 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000638 return false;
639 }
640
641 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000642 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000643 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000644 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000645 case AsmToken::Integer: {
646 SMLoc Loc = getTok().getLoc();
647 int64_t IntVal = getTok().getIntVal();
648 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000649 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000650 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000651 // Look for 'b' or 'f' following an Integer as a directional label
652 if (Lexer.getKind() == AsmToken::Identifier) {
653 StringRef IDVal = getTok().getString();
654 if (IDVal == "f" || IDVal == "b"){
655 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
656 IDVal == "f" ? 1 : 0);
657 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
658 getContext());
659 if(IDVal == "b" && Sym->isUndefined())
660 return Error(Loc, "invalid reference to undefined symbol");
661 EndLoc = Lexer.getLoc();
662 Lex(); // Eat identifier.
663 }
664 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000665 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000666 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000667 case AsmToken::Real: {
668 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000669 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000670 Res = MCConstantExpr::Create(IntVal, getContext());
671 Lex(); // Eat token.
672 return false;
673 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000674 case AsmToken::Dot: {
675 // This is a '.' reference, which references the current PC. Emit a
676 // temporary label to the streamer and refer to it.
677 MCSymbol *Sym = Ctx.CreateTempSymbol();
678 Out.EmitLabel(Sym);
679 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
680 EndLoc = Lexer.getLoc();
681 Lex(); // Eat identifier.
682 return false;
683 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000684 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000685 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000686 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000687 case AsmToken::LBrac:
688 if (!PlatformParser->HasBracketExpressions())
689 return TokError("brackets expression not supported on this target");
690 Lex(); // Eat the '['.
691 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000692 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000693 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000694 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000695 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000696 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000697 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000698 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000699 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000700 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000701 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000702 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000703 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000704 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000705 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000706 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000707 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000708 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000709 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000710 }
711}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000712
Chris Lattnerb4307b32010-01-15 19:28:38 +0000713bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000714 SMLoc EndLoc;
715 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000716}
717
Daniel Dunbarcceba832010-09-17 02:47:07 +0000718const MCExpr *
719AsmParser::ApplyModifierToExpr(const MCExpr *E,
720 MCSymbolRefExpr::VariantKind Variant) {
721 // Recurse over the given expression, rebuilding it to apply the given variant
722 // if there is exactly one symbol.
723 switch (E->getKind()) {
724 case MCExpr::Target:
725 case MCExpr::Constant:
726 return 0;
727
728 case MCExpr::SymbolRef: {
729 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
730
731 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
732 TokError("invalid variant on expression '" +
733 getTok().getIdentifier() + "' (already modified)");
734 return E;
735 }
736
737 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
738 }
739
740 case MCExpr::Unary: {
741 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
742 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
743 if (!Sub)
744 return 0;
745 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
746 }
747
748 case MCExpr::Binary: {
749 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
750 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
751 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
752
753 if (!LHS && !RHS)
754 return 0;
755
756 if (!LHS) LHS = BE->getLHS();
757 if (!RHS) RHS = BE->getRHS();
758
759 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
760 }
761 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000762
763 assert(0 && "Invalid expression kind!");
764 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000765}
766
Chris Lattner74ec1a32009-06-22 06:32:03 +0000767/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000768///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000769/// expr ::= expr &&,|| expr -> lowest.
770/// expr ::= expr |,^,&,! expr
771/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
772/// expr ::= expr <<,>> expr
773/// expr ::= expr +,- expr
774/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000775/// expr ::= primaryexpr
776///
Chris Lattner54482b42010-01-15 19:39:23 +0000777bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000778 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000779 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000780 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
781 return true;
782
Daniel Dunbarcceba832010-09-17 02:47:07 +0000783 // As a special case, we support 'a op b @ modifier' by rewriting the
784 // expression to include the modifier. This is inefficient, but in general we
785 // expect users to use 'a@modifier op b'.
786 if (Lexer.getKind() == AsmToken::At) {
787 Lex();
788
789 if (Lexer.isNot(AsmToken::Identifier))
790 return TokError("unexpected symbol modifier following '@'");
791
792 MCSymbolRefExpr::VariantKind Variant =
793 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
794 if (Variant == MCSymbolRefExpr::VK_Invalid)
795 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
796
797 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
798 if (!ModifiedRes) {
799 return TokError("invalid modifier '" + getTok().getIdentifier() +
800 "' (no symbols present)");
801 return true;
802 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000803
Daniel Dunbarcceba832010-09-17 02:47:07 +0000804 Res = ModifiedRes;
805 Lex();
806 }
807
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000808 // Try to constant fold it up front, if possible.
809 int64_t Value;
810 if (Res->EvaluateAsAbsolute(Value))
811 Res = MCConstantExpr::Create(Value, getContext());
812
813 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000814}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000815
Chris Lattnerb4307b32010-01-15 19:28:38 +0000816bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000817 Res = 0;
818 return ParseParenExpr(Res, EndLoc) ||
819 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000820}
821
Daniel Dunbar475839e2009-06-29 20:37:27 +0000822bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000823 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000824
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000825 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000826 if (ParseExpression(Expr))
827 return true;
828
Daniel Dunbare00b0112009-10-16 01:57:52 +0000829 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000830 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000831
832 return false;
833}
834
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000835static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000836 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000837 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000838 default:
839 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000840
Jim Grosbachfbe16812011-08-20 16:24:13 +0000841 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000842 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000843 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000844 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000845 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000846 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000847 return 1;
848
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000849
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000850 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000851 //
852 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000853 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000854 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000855 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000856 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000857 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000858 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000859 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000860 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000861 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000862
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000863 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000864 case AsmToken::EqualEqual:
865 Kind = MCBinaryExpr::EQ;
866 return 3;
867 case AsmToken::ExclaimEqual:
868 case AsmToken::LessGreater:
869 Kind = MCBinaryExpr::NE;
870 return 3;
871 case AsmToken::Less:
872 Kind = MCBinaryExpr::LT;
873 return 3;
874 case AsmToken::LessEqual:
875 Kind = MCBinaryExpr::LTE;
876 return 3;
877 case AsmToken::Greater:
878 Kind = MCBinaryExpr::GT;
879 return 3;
880 case AsmToken::GreaterEqual:
881 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000882 return 3;
883
Jim Grosbachfbe16812011-08-20 16:24:13 +0000884 // Intermediate Precedence: <<, >>
885 case AsmToken::LessLess:
886 Kind = MCBinaryExpr::Shl;
887 return 4;
888 case AsmToken::GreaterGreater:
889 Kind = MCBinaryExpr::Shr;
890 return 4;
891
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000892 // High Intermediate Precedence: +, -
893 case AsmToken::Plus:
894 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000895 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000896 case AsmToken::Minus:
897 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000898 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000899
Jim Grosbachfbe16812011-08-20 16:24:13 +0000900 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000901 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000902 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000903 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000904 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000905 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000906 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000907 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000908 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000909 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000910 }
911}
912
913
914/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
915/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000916bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
917 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000918 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000919 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000920 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000921
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000922 // If the next token is lower precedence than we are allowed to eat, return
923 // successfully with what we ate already.
924 if (TokPrec < Precedence)
925 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000926
Sean Callanan79ed1a82010-01-19 20:22:31 +0000927 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000928
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000929 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000930 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000931 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000932
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000933 // If BinOp binds less tightly with RHS than the operator after RHS, let
934 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000935 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000936 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000937 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000938 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000939 }
940
Daniel Dunbar475839e2009-06-29 20:37:27 +0000941 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000942 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000943 }
944}
945
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000946
947
948
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000949/// ParseStatement:
950/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000951/// ::= Label* Directive ...Operands... EndOfStatement
952/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000953bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000954 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000955 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000956 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000957 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000958 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000959
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000960 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000961 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000962 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000963 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000964 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000965 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000966 if (Lexer.is(AsmToken::Hash))
967 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000968
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000969 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000970 if (Lexer.is(AsmToken::Integer)) {
971 LocalLabelVal = getTok().getIntVal();
972 if (LocalLabelVal < 0) {
973 if (!TheCondState.Ignore)
974 return TokError("unexpected token at start of statement");
975 IDVal = "";
976 }
977 else {
978 IDVal = getTok().getString();
979 Lex(); // Consume the integer token to be used as an identifier token.
980 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000981 if (!TheCondState.Ignore)
982 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000983 }
984 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000985
986 } else if (Lexer.is(AsmToken::Dot)) {
987 // Treat '.' as a valid identifier in this context.
988 Lex();
989 IDVal = ".";
990
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000991 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000992 if (!TheCondState.Ignore)
993 return TokError("unexpected token at start of statement");
994 IDVal = "";
995 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000996
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000997
Chris Lattner7834fac2010-04-17 18:14:27 +0000998 // Handle conditional assembly here before checking for skipping. We
999 // have to do this so that .endif isn't skipped in a ".if 0" block for
1000 // example.
1001 if (IDVal == ".if")
1002 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001003 if (IDVal == ".ifdef")
1004 return ParseDirectiveIfdef(IDLoc, true);
1005 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1006 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001007 if (IDVal == ".elseif")
1008 return ParseDirectiveElseIf(IDLoc);
1009 if (IDVal == ".else")
1010 return ParseDirectiveElse(IDLoc);
1011 if (IDVal == ".endif")
1012 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001013
Chris Lattner7834fac2010-04-17 18:14:27 +00001014 // If we are in a ".if 0" block, ignore this statement.
1015 if (TheCondState.Ignore) {
1016 EatToEndOfStatement();
1017 return false;
1018 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001019
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001020 // FIXME: Recurse on local labels?
1021
1022 // See what kind of statement we have.
1023 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001024 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001025 CheckForValidSection();
1026
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001027 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001028 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001029
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001030 // Diagnose attempt to use '.' as a label.
1031 if (IDVal == ".")
1032 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1033
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001034 // Diagnose attempt to use a variable as a label.
1035 //
1036 // FIXME: Diagnostics. Note the location of the definition as a label.
1037 // FIXME: This doesn't diagnose assignment to a symbol which has been
1038 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001039 MCSymbol *Sym;
1040 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001041 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001042 else
1043 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001044 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001045 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001046
Daniel Dunbar959fd882009-08-26 22:13:22 +00001047 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001048 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001049
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001050 // Consume any end of statement token, if present, to avoid spurious
1051 // AddBlankLine calls().
1052 if (Lexer.is(AsmToken::EndOfStatement)) {
1053 Lex();
1054 if (Lexer.is(AsmToken::Eof))
1055 return false;
1056 }
1057
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001058 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001059 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001060
Daniel Dunbar3f872332009-07-28 16:08:33 +00001061 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001062 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001063 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001064
Nico Weber4c4c7322011-01-28 03:04:41 +00001065 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001066
1067 default: // Normal instruction or directive.
1068 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001069 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001070
1071 // If macros are enabled, check to see if this is a macro instantiation.
1072 if (MacrosEnabled)
1073 if (const Macro *M = MacroMap.lookup(IDVal))
1074 return HandleMacroEntry(IDVal, IDLoc, M);
1075
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001076 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001077 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001078 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001079 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001080 return ParseDirectiveSet(IDVal, true);
1081 if (IDVal == ".equiv")
1082 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001083
Daniel Dunbara0d14262009-06-24 23:30:00 +00001084 // Data directives
1085
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001086 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001087 return ParseDirectiveAscii(IDVal, false);
1088 if (IDVal == ".asciz" || IDVal == ".string")
1089 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001090
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001091 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001092 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001093 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001094 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001095 if (IDVal == ".value")
1096 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001097 if (IDVal == ".2byte")
1098 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001099 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001100 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001101 if (IDVal == ".int")
1102 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001103 if (IDVal == ".4byte")
1104 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001105 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001106 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001107 if (IDVal == ".8byte")
1108 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001109 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001110 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1111 if (IDVal == ".double")
1112 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001113
Eli Friedman5d68ec22010-07-19 04:17:25 +00001114 if (IDVal == ".align") {
1115 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1116 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1117 }
1118 if (IDVal == ".align32") {
1119 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1120 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1121 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001122 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001123 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001124 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001125 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001126 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001127 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001128 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001129 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001130 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001131 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001132 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001133 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1134
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001135 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001136 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001137
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001138 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001139 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001140 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001141 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001142 if (IDVal == ".zero")
1143 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001144
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001145 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001146
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001147 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001148 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001149 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001150 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001151 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001152 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001153 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001154 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001155 if (IDVal == ".symbol_resolver")
1156 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001157 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001158 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001159 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001160 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001161 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001162 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001163 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001164 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001165 if (IDVal == ".weak_def_can_be_hidden")
1166 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001167
Hans Wennborg5cc64912011-06-18 13:51:54 +00001168 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001169 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001170 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001171 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001172
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001173 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001174 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001175 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001176 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001177
Evan Chengbd27f5a2011-07-27 00:38:12 +00001178 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001179 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001180
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001181 // Look up the handler in the handler table.
1182 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1183 DirectiveMap.lookup(IDVal);
1184 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001185 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001186
Kevin Enderby9c656452009-09-10 20:51:44 +00001187 // Target hook for parsing target specific directives.
1188 if (!getTargetParser().ParseDirective(ID))
1189 return false;
1190
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001191 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001192 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001193 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001194 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001195
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001196 CheckForValidSection();
1197
Chris Lattnera7f13542010-05-19 23:34:33 +00001198 // Canonicalize the opcode to lower case.
1199 SmallString<128> Opcode;
1200 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1201 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001202
Chris Lattner98986712010-01-14 22:21:20 +00001203 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001204 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001205 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001206
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001207 // Dump the parsed representation, if requested.
1208 if (getShowParsedOperands()) {
1209 SmallString<256> Str;
1210 raw_svector_ostream OS(Str);
1211 OS << "parsed instruction: [";
1212 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1213 if (i != 0)
1214 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001215 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001216 }
1217 OS << "]";
1218
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001219 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001220 }
1221
Kevin Enderby613b7572011-11-01 22:27:22 +00001222 // If we are generating dwarf for assembly source files and the current
1223 // section is the initial text section then generate a .loc directive for
1224 // the instruction.
1225 if (!HadError && getContext().getGenDwarfForAssembly() &&
1226 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1227 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1228 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1229 0, DWARF2_LINE_DEFAULT_IS_STMT ?
1230 DWARF2_FLAG_IS_STMT : 0, 0, 0,
1231 StringRef());
1232 }
1233
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001234 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001235 if (!HadError)
1236 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1237 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001238
Chris Lattner98986712010-01-14 22:21:20 +00001239 // Free any parsed operands.
1240 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1241 delete ParsedOperands[i];
1242
Chris Lattnercbf8a982010-09-11 16:18:25 +00001243 // Don't skip the rest of the line, the instruction parser is responsible for
1244 // that.
1245 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001246}
Chris Lattner9a023f72009-06-24 04:43:34 +00001247
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001248/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1249/// since they may not be able to be tokenized to get to the end of line token.
1250void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001251 if (!Lexer.is(AsmToken::EndOfStatement))
1252 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001253 // Eat EOL.
1254 Lex();
1255}
1256
1257/// ParseCppHashLineFilenameComment as this:
1258/// ::= # number "filename"
1259/// or just as a full line comment if it doesn't have a number and a string.
1260bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1261 Lex(); // Eat the hash token.
1262
1263 if (getLexer().isNot(AsmToken::Integer)) {
1264 // Consume the line since in cases it is not a well-formed line directive,
1265 // as if were simply a full line comment.
1266 EatToEndOfLine();
1267 return false;
1268 }
1269
1270 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001271 Lex();
1272
1273 if (getLexer().isNot(AsmToken::String)) {
1274 EatToEndOfLine();
1275 return false;
1276 }
1277
1278 StringRef Filename = getTok().getString();
1279 // Get rid of the enclosing quotes.
1280 Filename = Filename.substr(1, Filename.size()-2);
1281
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001282 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1283 CppHashLoc = L;
1284 CppHashFilename = Filename;
1285 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001286
1287 // Ignore any trailing characters, they're just comment.
1288 EatToEndOfLine();
1289 return false;
1290}
1291
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001292/// DiagHandler - will use the the last parsed cpp hash line filename comment
1293/// for the Filename and LineNo if any in the diagnostic.
1294void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1295 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1296 raw_ostream &OS = errs();
1297
1298 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1299 const SMLoc &DiagLoc = Diag.getLoc();
1300 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1301 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1302
1303 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1304 // before printing the message.
1305 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001306 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001307 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1308 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1309 }
1310
1311 // If we have not parsed a cpp hash line filename comment or the source
1312 // manager changed or buffer changed (like in a nested include) then just
1313 // print the normal diagnostic using its Filename and LineNo.
1314 if (!Parser->CppHashLineNumber ||
1315 &DiagSrcMgr != &Parser->SrcMgr ||
1316 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001317 if (Parser->SavedDiagHandler)
1318 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1319 else
1320 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001321 return;
1322 }
1323
1324 // Use the CppHashFilename and calculate a line number based on the
1325 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1326 // the diagnostic.
1327 const std::string Filename = Parser->CppHashFilename;
1328
1329 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1330 int CppHashLocLineNo =
1331 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1332 int LineNo = Parser->CppHashLineNumber - 1 +
1333 (DiagLocLineNo - CppHashLocLineNo);
1334
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001335 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1336 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001337 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001338 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001339
Benjamin Kramer04a04262011-10-16 10:48:29 +00001340 if (Parser->SavedDiagHandler)
1341 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1342 else
1343 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001344}
1345
Rafael Espindola65366442011-06-05 02:43:45 +00001346bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1347 const std::vector<StringRef> &Parameters,
1348 const std::vector<std::vector<AsmToken> > &A,
1349 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001350 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001351 unsigned NParameters = Parameters.size();
1352 if (NParameters != 0 && NParameters != A.size())
1353 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001354
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001355 while (!Body.empty()) {
1356 // Scan for the next substitution.
1357 std::size_t End = Body.size(), Pos = 0;
1358 for (; Pos != End; ++Pos) {
1359 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001360 if (!NParameters) {
1361 // This macro has no parameters, look for $0, $1, etc.
1362 if (Body[Pos] != '$' || Pos + 1 == End)
1363 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001364
Rafael Espindola65366442011-06-05 02:43:45 +00001365 char Next = Body[Pos + 1];
1366 if (Next == '$' || Next == 'n' || isdigit(Next))
1367 break;
1368 } else {
1369 // This macro has parameters, look for \foo, \bar, etc.
1370 if (Body[Pos] == '\\' && Pos + 1 != End)
1371 break;
1372 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001373 }
1374
1375 // Add the prefix.
1376 OS << Body.slice(0, Pos);
1377
1378 // Check if we reached the end.
1379 if (Pos == End)
1380 break;
1381
Rafael Espindola65366442011-06-05 02:43:45 +00001382 if (!NParameters) {
1383 switch (Body[Pos+1]) {
1384 // $$ => $
1385 case '$':
1386 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001387 break;
1388
Rafael Espindola65366442011-06-05 02:43:45 +00001389 // $n => number of arguments
1390 case 'n':
1391 OS << A.size();
1392 break;
1393
1394 // $[0-9] => argument
1395 default: {
1396 // Missing arguments are ignored.
1397 unsigned Index = Body[Pos+1] - '0';
1398 if (Index >= A.size())
1399 break;
1400
1401 // Otherwise substitute with the token values, with spaces eliminated.
1402 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1403 ie = A[Index].end(); it != ie; ++it)
1404 OS << it->getString();
1405 break;
1406 }
1407 }
1408 Pos += 2;
1409 } else {
1410 unsigned I = Pos + 1;
1411 while (isalnum(Body[I]) && I + 1 != End)
1412 ++I;
1413
1414 const char *Begin = Body.data() + Pos +1;
1415 StringRef Argument(Begin, I - (Pos +1));
1416 unsigned Index = 0;
1417 for (; Index < NParameters; ++Index)
1418 if (Parameters[Index] == Argument)
1419 break;
1420
1421 // FIXME: We should error at the macro definition.
1422 if (Index == NParameters)
1423 return Error(L, "Parameter not found");
1424
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001425 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1426 ie = A[Index].end(); it != ie; ++it)
1427 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001428
Rafael Espindola65366442011-06-05 02:43:45 +00001429 Pos += 1 + Argument.size();
1430 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001431 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001432 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001433 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001434
1435 // We include the .endmacro in the buffer as our queue to exit the macro
1436 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001437 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001438 return false;
1439}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001440
Rafael Espindola65366442011-06-05 02:43:45 +00001441MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1442 MemoryBuffer *I)
1443 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1444{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001445}
1446
1447bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1448 const Macro *M) {
1449 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1450 // this, although we should protect against infinite loops.
1451 if (ActiveMacros.size() == 20)
1452 return TokError("macros cannot be nested more than 20 levels deep");
1453
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001454 // Parse the macro instantiation arguments.
1455 std::vector<std::vector<AsmToken> > MacroArguments;
1456 MacroArguments.push_back(std::vector<AsmToken>());
1457 unsigned ParenLevel = 0;
1458 for (;;) {
1459 if (Lexer.is(AsmToken::Eof))
1460 return TokError("unexpected token in macro instantiation");
1461 if (Lexer.is(AsmToken::EndOfStatement))
1462 break;
1463
1464 // If we aren't inside parentheses and this is a comma, start a new token
1465 // list.
1466 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1467 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001468 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001469 // Adjust the current parentheses level.
1470 if (Lexer.is(AsmToken::LParen))
1471 ++ParenLevel;
1472 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1473 --ParenLevel;
1474
1475 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001476 MacroArguments.back().push_back(getTok());
1477 }
1478 Lex();
1479 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001480
Rafael Espindola65366442011-06-05 02:43:45 +00001481 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1482 // to hold the macro body with substitutions.
1483 SmallString<256> Buf;
1484 StringRef Body = M->Body;
1485
1486 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1487 return true;
1488
1489 MemoryBuffer *Instantiation =
1490 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1491
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001492 // Create the macro instantiation object and add to the current macro
1493 // instantiation stack.
1494 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001495 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001496 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001497 ActiveMacros.push_back(MI);
1498
1499 // Jump to the macro instantiation and prime the lexer.
1500 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1501 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1502 Lex();
1503
1504 return false;
1505}
1506
1507void AsmParser::HandleMacroExit() {
1508 // Jump to the EndOfStatement we should return to, and consume it.
1509 JumpToLoc(ActiveMacros.back()->ExitLoc);
1510 Lex();
1511
1512 // Pop the instantiation entry.
1513 delete ActiveMacros.back();
1514 ActiveMacros.pop_back();
1515}
1516
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001517static void MarkUsed(const MCExpr *Value) {
1518 switch (Value->getKind()) {
1519 case MCExpr::Binary:
1520 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1521 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1522 break;
1523 case MCExpr::Target:
1524 case MCExpr::Constant:
1525 break;
1526 case MCExpr::SymbolRef: {
1527 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1528 break;
1529 }
1530 case MCExpr::Unary:
1531 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1532 break;
1533 }
1534}
1535
Nico Weber4c4c7322011-01-28 03:04:41 +00001536bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001537 // FIXME: Use better location, we should use proper tokens.
1538 SMLoc EqualLoc = Lexer.getLoc();
1539
Daniel Dunbar821e3332009-08-31 08:09:28 +00001540 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001541 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001542 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001543
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001544 MarkUsed(Value);
1545
Daniel Dunbar3f872332009-07-28 16:08:33 +00001546 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001547 return TokError("unexpected token in assignment");
1548
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001549 // Error on assignment to '.'.
1550 if (Name == ".") {
1551 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1552 "(use '.space' or '.org').)"));
1553 }
1554
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001555 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001556 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001557
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001558 // Validate that the LHS is allowed to be a variable (either it has not been
1559 // used as a symbol, or it is an absolute symbol).
1560 MCSymbol *Sym = getContext().LookupSymbol(Name);
1561 if (Sym) {
1562 // Diagnose assignment to a label.
1563 //
1564 // FIXME: Diagnostics. Note the location of the definition as a label.
1565 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001566 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001567 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001568 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001569 return Error(EqualLoc, "redefinition of '" + Name + "'");
1570 else if (!Sym->isVariable())
1571 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001572 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001573 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1574 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001575
1576 // Don't count these checks as uses.
1577 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001578 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001579 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001580
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001581 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001582
1583 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001584 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001585
1586 return false;
1587}
1588
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001589/// ParseIdentifier:
1590/// ::= identifier
1591/// ::= string
1592bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001593 // The assembler has relaxed rules for accepting identifiers, in particular we
1594 // allow things like '.globl $foo', which would normally be separate
1595 // tokens. At this level, we have already lexed so we cannot (currently)
1596 // handle this as a context dependent token, instead we detect adjacent tokens
1597 // and return the combined identifier.
1598 if (Lexer.is(AsmToken::Dollar)) {
1599 SMLoc DollarLoc = getLexer().getLoc();
1600
1601 // Consume the dollar sign, and check for a following identifier.
1602 Lex();
1603 if (Lexer.isNot(AsmToken::Identifier))
1604 return true;
1605
1606 // We have a '$' followed by an identifier, make sure they are adjacent.
1607 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1608 return true;
1609
1610 // Construct the joined identifier and consume the token.
1611 Res = StringRef(DollarLoc.getPointer(),
1612 getTok().getIdentifier().size() + 1);
1613 Lex();
1614 return false;
1615 }
1616
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001617 if (Lexer.isNot(AsmToken::Identifier) &&
1618 Lexer.isNot(AsmToken::String))
1619 return true;
1620
Sean Callanan18b83232010-01-19 21:44:56 +00001621 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001622
Sean Callanan79ed1a82010-01-19 20:22:31 +00001623 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001624
1625 return false;
1626}
1627
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001628/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001629/// ::= .equ identifier ',' expression
1630/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001631/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001632bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001633 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001634
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001635 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001636 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001637
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001638 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001639 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001640 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001641
Nico Weber4c4c7322011-01-28 03:04:41 +00001642 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001643}
1644
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001645bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001646 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001647
1648 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001649 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001650 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1651 if (Str[i] != '\\') {
1652 Data += Str[i];
1653 continue;
1654 }
1655
1656 // Recognize escaped characters. Note that this escape semantics currently
1657 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1658 ++i;
1659 if (i == e)
1660 return TokError("unexpected backslash at end of string");
1661
1662 // Recognize octal sequences.
1663 if ((unsigned) (Str[i] - '0') <= 7) {
1664 // Consume up to three octal characters.
1665 unsigned Value = Str[i] - '0';
1666
1667 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1668 ++i;
1669 Value = Value * 8 + (Str[i] - '0');
1670
1671 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1672 ++i;
1673 Value = Value * 8 + (Str[i] - '0');
1674 }
1675 }
1676
1677 if (Value > 255)
1678 return TokError("invalid octal escape sequence (out of range)");
1679
1680 Data += (unsigned char) Value;
1681 continue;
1682 }
1683
1684 // Otherwise recognize individual escapes.
1685 switch (Str[i]) {
1686 default:
1687 // Just reject invalid escape sequences for now.
1688 return TokError("invalid escape sequence (unrecognized character)");
1689
1690 case 'b': Data += '\b'; break;
1691 case 'f': Data += '\f'; break;
1692 case 'n': Data += '\n'; break;
1693 case 'r': Data += '\r'; break;
1694 case 't': Data += '\t'; break;
1695 case '"': Data += '"'; break;
1696 case '\\': Data += '\\'; break;
1697 }
1698 }
1699
1700 return false;
1701}
1702
Daniel Dunbara0d14262009-06-24 23:30:00 +00001703/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001704/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1705bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001706 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001707 CheckForValidSection();
1708
Daniel Dunbara0d14262009-06-24 23:30:00 +00001709 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001710 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001711 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001712
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001713 std::string Data;
1714 if (ParseEscapedString(Data))
1715 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001716
1717 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001718 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001719 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1720
Sean Callanan79ed1a82010-01-19 20:22:31 +00001721 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001722
1723 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001724 break;
1725
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001726 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001727 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001728 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001729 }
1730 }
1731
Sean Callanan79ed1a82010-01-19 20:22:31 +00001732 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001733 return false;
1734}
1735
1736/// ParseDirectiveValue
1737/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1738bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001739 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001740 CheckForValidSection();
1741
Daniel Dunbara0d14262009-06-24 23:30:00 +00001742 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001743 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001744 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001745 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001746 return true;
1747
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001748 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001749 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1750 assert(Size <= 8 && "Invalid size");
1751 uint64_t IntValue = MCE->getValue();
1752 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1753 return Error(ExprLoc, "literal value out of range for directive");
1754 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1755 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001756 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001757
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001758 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001759 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001760
Daniel Dunbara0d14262009-06-24 23:30:00 +00001761 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001762 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001763 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001764 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001765 }
1766 }
1767
Sean Callanan79ed1a82010-01-19 20:22:31 +00001768 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001769 return false;
1770}
1771
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001772/// ParseDirectiveRealValue
1773/// ::= (.single | .double) [ expression (, expression)* ]
1774bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1775 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1776 CheckForValidSection();
1777
1778 for (;;) {
1779 // We don't truly support arithmetic on floating point expressions, so we
1780 // have to manually parse unary prefixes.
1781 bool IsNeg = false;
1782 if (getLexer().is(AsmToken::Minus)) {
1783 Lex();
1784 IsNeg = true;
1785 } else if (getLexer().is(AsmToken::Plus))
1786 Lex();
1787
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001788 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001789 getLexer().isNot(AsmToken::Real) &&
1790 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001791 return TokError("unexpected token in directive");
1792
1793 // Convert to an APFloat.
1794 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001795 StringRef IDVal = getTok().getString();
1796 if (getLexer().is(AsmToken::Identifier)) {
1797 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1798 Value = APFloat::getInf(Semantics);
1799 else if (!IDVal.compare_lower("nan"))
1800 Value = APFloat::getNaN(Semantics, false, ~0);
1801 else
1802 return TokError("invalid floating point literal");
1803 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001804 APFloat::opInvalidOp)
1805 return TokError("invalid floating point literal");
1806 if (IsNeg)
1807 Value.changeSign();
1808
1809 // Consume the numeric token.
1810 Lex();
1811
1812 // Emit the value as an integer.
1813 APInt AsInt = Value.bitcastToAPInt();
1814 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1815 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1816
1817 if (getLexer().is(AsmToken::EndOfStatement))
1818 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001819
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001820 if (getLexer().isNot(AsmToken::Comma))
1821 return TokError("unexpected token in directive");
1822 Lex();
1823 }
1824 }
1825
1826 Lex();
1827 return false;
1828}
1829
Daniel Dunbara0d14262009-06-24 23:30:00 +00001830/// ParseDirectiveSpace
1831/// ::= .space expression [ , expression ]
1832bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001833 CheckForValidSection();
1834
Daniel Dunbara0d14262009-06-24 23:30:00 +00001835 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001836 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001837 return true;
1838
1839 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001840 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1841 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001842 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001843 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001844
Daniel Dunbar475839e2009-06-29 20:37:27 +00001845 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001846 return true;
1847
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001848 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001849 return TokError("unexpected token in '.space' directive");
1850 }
1851
Sean Callanan79ed1a82010-01-19 20:22:31 +00001852 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001853
1854 if (NumBytes <= 0)
1855 return TokError("invalid number of bytes in '.space' directive");
1856
1857 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001858 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001859
1860 return false;
1861}
1862
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001863/// ParseDirectiveZero
1864/// ::= .zero expression
1865bool AsmParser::ParseDirectiveZero() {
1866 CheckForValidSection();
1867
1868 int64_t NumBytes;
1869 if (ParseAbsoluteExpression(NumBytes))
1870 return true;
1871
Rafael Espindolae452b172010-10-05 19:42:57 +00001872 int64_t Val = 0;
1873 if (getLexer().is(AsmToken::Comma)) {
1874 Lex();
1875 if (ParseAbsoluteExpression(Val))
1876 return true;
1877 }
1878
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001879 if (getLexer().isNot(AsmToken::EndOfStatement))
1880 return TokError("unexpected token in '.zero' directive");
1881
1882 Lex();
1883
Rafael Espindolae452b172010-10-05 19:42:57 +00001884 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001885
1886 return false;
1887}
1888
Daniel Dunbara0d14262009-06-24 23:30:00 +00001889/// ParseDirectiveFill
1890/// ::= .fill expression , expression , expression
1891bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001892 CheckForValidSection();
1893
Daniel Dunbara0d14262009-06-24 23:30:00 +00001894 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001895 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001896 return true;
1897
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001898 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001899 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001900 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001901
Daniel Dunbara0d14262009-06-24 23:30:00 +00001902 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001903 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001904 return true;
1905
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001906 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001907 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001908 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001909
Daniel Dunbara0d14262009-06-24 23:30:00 +00001910 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001911 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001912 return true;
1913
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001914 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001915 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001916
Sean Callanan79ed1a82010-01-19 20:22:31 +00001917 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001918
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001919 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1920 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001921
1922 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001923 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001924
1925 return false;
1926}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001927
1928/// ParseDirectiveOrg
1929/// ::= .org expression [ , expression ]
1930bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001931 CheckForValidSection();
1932
Daniel Dunbar821e3332009-08-31 08:09:28 +00001933 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001934 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001935 return true;
1936
1937 // Parse optional fill expression.
1938 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001939 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1940 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001941 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001942 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001943
Daniel Dunbar475839e2009-06-29 20:37:27 +00001944 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001945 return true;
1946
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001947 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001948 return TokError("unexpected token in '.org' directive");
1949 }
1950
Sean Callanan79ed1a82010-01-19 20:22:31 +00001951 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001952
1953 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1954 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001955 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001956
1957 return false;
1958}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001959
1960/// ParseDirectiveAlign
1961/// ::= {.align, ...} expression [ , expression [ , expression ]]
1962bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001963 CheckForValidSection();
1964
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001965 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001966 int64_t Alignment;
1967 if (ParseAbsoluteExpression(Alignment))
1968 return true;
1969
1970 SMLoc MaxBytesLoc;
1971 bool HasFillExpr = false;
1972 int64_t FillExpr = 0;
1973 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001974 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1975 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001976 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001977 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001978
1979 // The fill expression can be omitted while specifying a maximum number of
1980 // alignment bytes, e.g:
1981 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001982 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001983 HasFillExpr = true;
1984 if (ParseAbsoluteExpression(FillExpr))
1985 return true;
1986 }
1987
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001988 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1989 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001990 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001991 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001992
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001993 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001994 if (ParseAbsoluteExpression(MaxBytesToFill))
1995 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001996
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001997 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001998 return TokError("unexpected token in directive");
1999 }
2000 }
2001
Sean Callanan79ed1a82010-01-19 20:22:31 +00002002 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002003
Daniel Dunbar648ac512010-05-17 21:54:30 +00002004 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002005 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002006
2007 // Compute alignment in bytes.
2008 if (IsPow2) {
2009 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002010 if (Alignment >= 32) {
2011 Error(AlignmentLoc, "invalid alignment value");
2012 Alignment = 31;
2013 }
2014
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002015 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002016 }
2017
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002018 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002019 if (MaxBytesLoc.isValid()) {
2020 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002021 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2022 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002023 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002024 }
2025
2026 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002027 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2028 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002029 MaxBytesToFill = 0;
2030 }
2031 }
2032
Daniel Dunbar648ac512010-05-17 21:54:30 +00002033 // Check whether we should use optimal code alignment for this .align
2034 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002035 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002036 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2037 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002038 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002039 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002040 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002041 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2042 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002043 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002044
2045 return false;
2046}
2047
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002048/// ParseDirectiveSymbolAttribute
2049/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002050bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002051 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002052 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002053 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002054 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002055
2056 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002057 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002058
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002059 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002060
Jim Grosbach10ec6502011-09-15 17:56:49 +00002061 // Assembler local symbols don't make any sense here. Complain loudly.
2062 if (Sym->isTemporary())
2063 return Error(Loc, "non-local symbol required in directive");
2064
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002065 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002066
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002067 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002068 break;
2069
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002070 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002071 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002072 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002073 }
2074 }
2075
Sean Callanan79ed1a82010-01-19 20:22:31 +00002076 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002077 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002078}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002079
2080/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002081/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2082bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002083 CheckForValidSection();
2084
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002085 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002086 StringRef Name;
2087 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002088 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002089
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002090 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002091 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002092
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002093 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002094 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002095 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002096
2097 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002098 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002099 if (ParseAbsoluteExpression(Size))
2100 return true;
2101
2102 int64_t Pow2Alignment = 0;
2103 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002104 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002105 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002106 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002107 if (ParseAbsoluteExpression(Pow2Alignment))
2108 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002109
Chris Lattner258281d2010-01-19 06:22:22 +00002110 // If this target takes alignments in bytes (not log) validate and convert.
2111 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2112 if (!isPowerOf2_64(Pow2Alignment))
2113 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2114 Pow2Alignment = Log2_64(Pow2Alignment);
2115 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002116 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002117
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002118 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002119 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002120
Sean Callanan79ed1a82010-01-19 20:22:31 +00002121 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002122
Chris Lattner1fc3d752009-07-09 17:25:12 +00002123 // NOTE: a size of zero for a .comm should create a undefined symbol
2124 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002125 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002126 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2127 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002128
Eric Christopherc260a3e2010-05-14 01:38:54 +00002129 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002130 // may internally end up wanting an alignment in bytes.
2131 // FIXME: Diagnose overflow.
2132 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002133 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2134 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002135
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002136 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002137 return Error(IDLoc, "invalid symbol redefinition");
2138
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002139 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002140 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002141 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002142 getStreamer().EmitZerofill(Ctx.getMachOSection(
2143 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2144 0, SectionKind::getBSS()),
2145 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002146 return false;
2147 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002148
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002149 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002150 return false;
2151}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002152
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002153/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002154/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002155bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002156 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002157 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002158
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002159 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002160 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002161 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002162
Sean Callanan79ed1a82010-01-19 20:22:31 +00002163 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002164
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002165 if (Str.empty())
2166 Error(Loc, ".abort detected. Assembly stopping.");
2167 else
2168 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002169 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002170
2171 return false;
2172}
Kevin Enderby71148242009-07-14 21:35:03 +00002173
Kevin Enderby1f049b22009-07-14 23:21:55 +00002174/// ParseDirectiveInclude
2175/// ::= .include "filename"
2176bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002177 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002178 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002179
Sean Callanan18b83232010-01-19 21:44:56 +00002180 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002181 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002182 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002183
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002184 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002185 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002186
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002187 // Strip the quotes.
2188 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002189
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002190 // Attempt to switch the lexer to the included file before consuming the end
2191 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002192 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002193 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002194 return true;
2195 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002196
2197 return false;
2198}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002199
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002200/// ParseDirectiveIf
2201/// ::= .if expression
2202bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002203 TheCondStack.push_back(TheCondState);
2204 TheCondState.TheCond = AsmCond::IfCond;
2205 if(TheCondState.Ignore) {
2206 EatToEndOfStatement();
2207 }
2208 else {
2209 int64_t ExprValue;
2210 if (ParseAbsoluteExpression(ExprValue))
2211 return true;
2212
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002213 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002214 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002215
Sean Callanan79ed1a82010-01-19 20:22:31 +00002216 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002217
2218 TheCondState.CondMet = ExprValue;
2219 TheCondState.Ignore = !TheCondState.CondMet;
2220 }
2221
2222 return false;
2223}
2224
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002225bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2226 StringRef Name;
2227 TheCondStack.push_back(TheCondState);
2228 TheCondState.TheCond = AsmCond::IfCond;
2229
2230 if (TheCondState.Ignore) {
2231 EatToEndOfStatement();
2232 } else {
2233 if (ParseIdentifier(Name))
2234 return TokError("expected identifier after '.ifdef'");
2235
2236 Lex();
2237
2238 MCSymbol *Sym = getContext().LookupSymbol(Name);
2239
2240 if (expect_defined)
2241 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2242 else
2243 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2244 TheCondState.Ignore = !TheCondState.CondMet;
2245 }
2246
2247 return false;
2248}
2249
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002250/// ParseDirectiveElseIf
2251/// ::= .elseif expression
2252bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2253 if (TheCondState.TheCond != AsmCond::IfCond &&
2254 TheCondState.TheCond != AsmCond::ElseIfCond)
2255 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2256 " an .elseif");
2257 TheCondState.TheCond = AsmCond::ElseIfCond;
2258
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002259 bool LastIgnoreState = false;
2260 if (!TheCondStack.empty())
2261 LastIgnoreState = TheCondStack.back().Ignore;
2262 if (LastIgnoreState || TheCondState.CondMet) {
2263 TheCondState.Ignore = true;
2264 EatToEndOfStatement();
2265 }
2266 else {
2267 int64_t ExprValue;
2268 if (ParseAbsoluteExpression(ExprValue))
2269 return true;
2270
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002271 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002272 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002273
Sean Callanan79ed1a82010-01-19 20:22:31 +00002274 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002275 TheCondState.CondMet = ExprValue;
2276 TheCondState.Ignore = !TheCondState.CondMet;
2277 }
2278
2279 return false;
2280}
2281
2282/// ParseDirectiveElse
2283/// ::= .else
2284bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002285 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002286 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002287
Sean Callanan79ed1a82010-01-19 20:22:31 +00002288 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002289
2290 if (TheCondState.TheCond != AsmCond::IfCond &&
2291 TheCondState.TheCond != AsmCond::ElseIfCond)
2292 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2293 ".elseif");
2294 TheCondState.TheCond = AsmCond::ElseCond;
2295 bool LastIgnoreState = false;
2296 if (!TheCondStack.empty())
2297 LastIgnoreState = TheCondStack.back().Ignore;
2298 if (LastIgnoreState || TheCondState.CondMet)
2299 TheCondState.Ignore = true;
2300 else
2301 TheCondState.Ignore = false;
2302
2303 return false;
2304}
2305
2306/// ParseDirectiveEndIf
2307/// ::= .endif
2308bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002309 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002310 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002311
Sean Callanan79ed1a82010-01-19 20:22:31 +00002312 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002313
2314 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2315 TheCondStack.empty())
2316 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2317 ".else");
2318 if (!TheCondStack.empty()) {
2319 TheCondState = TheCondStack.back();
2320 TheCondStack.pop_back();
2321 }
2322
2323 return false;
2324}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002325
2326/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002327/// ::= .file [number] filename
2328/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002329bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002330 // FIXME: I'm not sure what this is.
2331 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002332 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002333 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002334 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002335 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002336
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002337 if (FileNumber < 1)
2338 return TokError("file number less than one");
2339 }
2340
Daniel Dunbareceec052010-07-12 17:45:27 +00002341 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002342 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002343
Nick Lewycky44d798d2011-10-17 23:05:28 +00002344 // Usually the directory and filename together, otherwise just the directory.
2345 StringRef Path = getTok().getString();
2346 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002347 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002348
Nick Lewycky44d798d2011-10-17 23:05:28 +00002349 StringRef Directory;
2350 StringRef Filename;
2351 if (getLexer().is(AsmToken::String)) {
2352 if (FileNumber == -1)
2353 return TokError("explicit path specified, but no file number");
2354 Filename = getTok().getString();
2355 Filename = Filename.substr(1, Filename.size()-2);
2356 Directory = Path;
2357 Lex();
2358 } else {
2359 Filename = Path;
2360 }
2361
Daniel Dunbareceec052010-07-12 17:45:27 +00002362 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002363 return TokError("unexpected token in '.file' directive");
2364
Kevin Enderby613b7572011-11-01 22:27:22 +00002365 if (getContext().getGenDwarfForAssembly() == true)
2366 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2367 "used to generate dwarf debug info for assembly code");
2368
Chris Lattnerd32e8032010-01-25 19:02:58 +00002369 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002370 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002371 else {
Nick Lewycky44d798d2011-10-17 23:05:28 +00002372 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002373 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002374 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002375
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002376 return false;
2377}
2378
2379/// ParseDirectiveLine
2380/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002381bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002382 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2383 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002384 return TokError("unexpected token in '.line' directive");
2385
Sean Callanan18b83232010-01-19 21:44:56 +00002386 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002387 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002388 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002389
2390 // FIXME: Do something with the .line.
2391 }
2392
Daniel Dunbareceec052010-07-12 17:45:27 +00002393 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002394 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002395
2396 return false;
2397}
2398
2399
2400/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002401/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002402/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2403/// The first number is a file number, must have been previously assigned with
2404/// a .file directive, the second number is the line number and optionally the
2405/// third number is a column position (zero if not specified). The remaining
2406/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002407bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002408
Daniel Dunbareceec052010-07-12 17:45:27 +00002409 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002410 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002411 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002412 if (FileNumber < 1)
2413 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002414 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002415 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002416 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002417
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002418 int64_t LineNumber = 0;
2419 if (getLexer().is(AsmToken::Integer)) {
2420 LineNumber = getTok().getIntVal();
2421 if (LineNumber < 1)
2422 return TokError("line number less than one in '.loc' directive");
2423 Lex();
2424 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002425
2426 int64_t ColumnPos = 0;
2427 if (getLexer().is(AsmToken::Integer)) {
2428 ColumnPos = getTok().getIntVal();
2429 if (ColumnPos < 0)
2430 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002431 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002432 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002433
Kevin Enderbyc0957932010-09-30 16:52:03 +00002434 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002435 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002436 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002437 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2438 for (;;) {
2439 if (getLexer().is(AsmToken::EndOfStatement))
2440 break;
2441
2442 StringRef Name;
2443 SMLoc Loc = getTok().getLoc();
2444 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002445 return TokError("unexpected token in '.loc' directive");
2446
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002447 if (Name == "basic_block")
2448 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2449 else if (Name == "prologue_end")
2450 Flags |= DWARF2_FLAG_PROLOGUE_END;
2451 else if (Name == "epilogue_begin")
2452 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2453 else if (Name == "is_stmt") {
2454 SMLoc Loc = getTok().getLoc();
2455 const MCExpr *Value;
2456 if (getParser().ParseExpression(Value))
2457 return true;
2458 // The expression must be the constant 0 or 1.
2459 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2460 int Value = MCE->getValue();
2461 if (Value == 0)
2462 Flags &= ~DWARF2_FLAG_IS_STMT;
2463 else if (Value == 1)
2464 Flags |= DWARF2_FLAG_IS_STMT;
2465 else
2466 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002467 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002468 else {
2469 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2470 }
2471 }
2472 else if (Name == "isa") {
2473 SMLoc Loc = getTok().getLoc();
2474 const MCExpr *Value;
2475 if (getParser().ParseExpression(Value))
2476 return true;
2477 // The expression must be a constant greater or equal to 0.
2478 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2479 int Value = MCE->getValue();
2480 if (Value < 0)
2481 return Error(Loc, "isa number less than zero");
2482 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002483 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002484 else {
2485 return Error(Loc, "isa number not a constant value");
2486 }
2487 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002488 else if (Name == "discriminator") {
2489 if (getParser().ParseAbsoluteExpression(Discriminator))
2490 return true;
2491 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002492 else {
2493 return Error(Loc, "unknown sub-directive in '.loc' directive");
2494 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002495
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002496 if (getLexer().is(AsmToken::EndOfStatement))
2497 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002498 }
2499 }
2500
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002501 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002502 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002503
2504 return false;
2505}
2506
Daniel Dunbar138abae2010-10-16 04:56:42 +00002507/// ParseDirectiveStabs
2508/// ::= .stabs string, number, number, number
2509bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2510 SMLoc DirectiveLoc) {
2511 return TokError("unsupported directive '" + Directive + "'");
2512}
2513
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002514/// ParseDirectiveCFISections
2515/// ::= .cfi_sections section [, section]
2516bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2517 SMLoc DirectiveLoc) {
2518 StringRef Name;
2519 bool EH = false;
2520 bool Debug = false;
2521
2522 if (getParser().ParseIdentifier(Name))
2523 return TokError("Expected an identifier");
2524
2525 if (Name == ".eh_frame")
2526 EH = true;
2527 else if (Name == ".debug_frame")
2528 Debug = true;
2529
2530 if (getLexer().is(AsmToken::Comma)) {
2531 Lex();
2532
2533 if (getParser().ParseIdentifier(Name))
2534 return TokError("Expected an identifier");
2535
2536 if (Name == ".eh_frame")
2537 EH = true;
2538 else if (Name == ".debug_frame")
2539 Debug = true;
2540 }
2541
2542 getStreamer().EmitCFISections(EH, Debug);
2543
2544 return false;
2545}
2546
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002547/// ParseDirectiveCFIStartProc
2548/// ::= .cfi_startproc
2549bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2550 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002551 getStreamer().EmitCFIStartProc();
2552 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002553}
2554
2555/// ParseDirectiveCFIEndProc
2556/// ::= .cfi_endproc
2557bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002558 getStreamer().EmitCFIEndProc();
2559 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002560}
2561
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002562/// ParseRegisterOrRegisterNumber - parse register name or number.
2563bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2564 SMLoc DirectiveLoc) {
2565 unsigned RegNo;
2566
Jim Grosbach6f888a82011-06-02 17:14:04 +00002567 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002568 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2569 DirectiveLoc))
2570 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002571 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002572 } else
2573 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002574
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002575 return false;
2576}
2577
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002578/// ParseDirectiveCFIDefCfa
2579/// ::= .cfi_def_cfa register, offset
2580bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2581 SMLoc DirectiveLoc) {
2582 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002583 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002584 return true;
2585
2586 if (getLexer().isNot(AsmToken::Comma))
2587 return TokError("unexpected token in directive");
2588 Lex();
2589
2590 int64_t Offset = 0;
2591 if (getParser().ParseAbsoluteExpression(Offset))
2592 return true;
2593
Rafael Espindola066c2f42011-04-12 23:59:07 +00002594 getStreamer().EmitCFIDefCfa(Register, Offset);
2595 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002596}
2597
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002598/// ParseDirectiveCFIDefCfaOffset
2599/// ::= .cfi_def_cfa_offset offset
2600bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2601 SMLoc DirectiveLoc) {
2602 int64_t Offset = 0;
2603 if (getParser().ParseAbsoluteExpression(Offset))
2604 return true;
2605
Rafael Espindola066c2f42011-04-12 23:59:07 +00002606 getStreamer().EmitCFIDefCfaOffset(Offset);
2607 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002608}
2609
2610/// ParseDirectiveCFIAdjustCfaOffset
2611/// ::= .cfi_adjust_cfa_offset adjustment
2612bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2613 SMLoc DirectiveLoc) {
2614 int64_t Adjustment = 0;
2615 if (getParser().ParseAbsoluteExpression(Adjustment))
2616 return true;
2617
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002618 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2619 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002620}
2621
2622/// ParseDirectiveCFIDefCfaRegister
2623/// ::= .cfi_def_cfa_register register
2624bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2625 SMLoc DirectiveLoc) {
2626 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002627 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002628 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002629
Rafael Espindola066c2f42011-04-12 23:59:07 +00002630 getStreamer().EmitCFIDefCfaRegister(Register);
2631 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002632}
2633
2634/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002635/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002636bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2637 int64_t Register = 0;
2638 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002639
2640 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002641 return true;
2642
2643 if (getLexer().isNot(AsmToken::Comma))
2644 return TokError("unexpected token in directive");
2645 Lex();
2646
2647 if (getParser().ParseAbsoluteExpression(Offset))
2648 return true;
2649
Rafael Espindola066c2f42011-04-12 23:59:07 +00002650 getStreamer().EmitCFIOffset(Register, Offset);
2651 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002652}
2653
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002654/// ParseDirectiveCFIRelOffset
2655/// ::= .cfi_rel_offset register, offset
2656bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2657 SMLoc DirectiveLoc) {
2658 int64_t Register = 0;
2659
2660 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2661 return true;
2662
2663 if (getLexer().isNot(AsmToken::Comma))
2664 return TokError("unexpected token in directive");
2665 Lex();
2666
2667 int64_t Offset = 0;
2668 if (getParser().ParseAbsoluteExpression(Offset))
2669 return true;
2670
Rafael Espindola25f492e2011-04-12 16:12:03 +00002671 getStreamer().EmitCFIRelOffset(Register, Offset);
2672 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002673}
2674
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002675static bool isValidEncoding(int64_t Encoding) {
2676 if (Encoding & ~0xff)
2677 return false;
2678
2679 if (Encoding == dwarf::DW_EH_PE_omit)
2680 return true;
2681
2682 const unsigned Format = Encoding & 0xf;
2683 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2684 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2685 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2686 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2687 return false;
2688
Rafael Espindolacaf11582010-12-29 04:31:26 +00002689 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002690 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002691 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002692 return false;
2693
2694 return true;
2695}
2696
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002697/// ParseDirectiveCFIPersonalityOrLsda
2698/// ::= .cfi_personality encoding, [symbol_name]
2699/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002700bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002701 SMLoc DirectiveLoc) {
2702 int64_t Encoding = 0;
2703 if (getParser().ParseAbsoluteExpression(Encoding))
2704 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002705 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002706 return false;
2707
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002708 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002709 return TokError("unsupported encoding.");
2710
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002711 if (getLexer().isNot(AsmToken::Comma))
2712 return TokError("unexpected token in directive");
2713 Lex();
2714
2715 StringRef Name;
2716 if (getParser().ParseIdentifier(Name))
2717 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002718
2719 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2720
2721 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002722 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002723 else {
2724 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002725 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002726 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002727 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002728}
2729
Rafael Espindolafe024d02010-12-28 18:36:23 +00002730/// ParseDirectiveCFIRememberState
2731/// ::= .cfi_remember_state
2732bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2733 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002734 getStreamer().EmitCFIRememberState();
2735 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002736}
2737
2738/// ParseDirectiveCFIRestoreState
2739/// ::= .cfi_remember_state
2740bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2741 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002742 getStreamer().EmitCFIRestoreState();
2743 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002744}
2745
Rafael Espindolac5754392011-04-12 15:31:05 +00002746/// ParseDirectiveCFISameValue
2747/// ::= .cfi_same_value register
2748bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2749 SMLoc DirectiveLoc) {
2750 int64_t Register = 0;
2751
2752 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2753 return true;
2754
2755 getStreamer().EmitCFISameValue(Register);
2756
2757 return false;
2758}
2759
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002760/// ParseDirectiveMacrosOnOff
2761/// ::= .macros_on
2762/// ::= .macros_off
2763bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2764 SMLoc DirectiveLoc) {
2765 if (getLexer().isNot(AsmToken::EndOfStatement))
2766 return Error(getLexer().getLoc(),
2767 "unexpected token in '" + Directive + "' directive");
2768
2769 getParser().MacrosEnabled = Directive == ".macros_on";
2770
2771 return false;
2772}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002773
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002774/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002775/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002776bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2777 SMLoc DirectiveLoc) {
2778 StringRef Name;
2779 if (getParser().ParseIdentifier(Name))
2780 return TokError("expected identifier in directive");
2781
Rafael Espindola65366442011-06-05 02:43:45 +00002782 std::vector<StringRef> Parameters;
2783 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2784 for(;;) {
2785 StringRef Parameter;
2786 if (getParser().ParseIdentifier(Parameter))
2787 return TokError("expected identifier in directive");
2788 Parameters.push_back(Parameter);
2789
2790 if (getLexer().isNot(AsmToken::Comma))
2791 break;
2792 Lex();
2793 }
2794 }
2795
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002796 if (getLexer().isNot(AsmToken::EndOfStatement))
2797 return TokError("unexpected token in '.macro' directive");
2798
2799 // Eat the end of statement.
2800 Lex();
2801
2802 AsmToken EndToken, StartToken = getTok();
2803
2804 // Lex the macro definition.
2805 for (;;) {
2806 // Check whether we have reached the end of the file.
2807 if (getLexer().is(AsmToken::Eof))
2808 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2809
2810 // Otherwise, check whether we have reach the .endmacro.
2811 if (getLexer().is(AsmToken::Identifier) &&
2812 (getTok().getIdentifier() == ".endm" ||
2813 getTok().getIdentifier() == ".endmacro")) {
2814 EndToken = getTok();
2815 Lex();
2816 if (getLexer().isNot(AsmToken::EndOfStatement))
2817 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2818 "' directive");
2819 break;
2820 }
2821
2822 // Otherwise, scan til the end of the statement.
2823 getParser().EatToEndOfStatement();
2824 }
2825
2826 if (getParser().MacroMap.lookup(Name)) {
2827 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2828 }
2829
2830 const char *BodyStart = StartToken.getLoc().getPointer();
2831 const char *BodyEnd = EndToken.getLoc().getPointer();
2832 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002833 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002834 return false;
2835}
2836
2837/// ParseDirectiveEndMacro
2838/// ::= .endm
2839/// ::= .endmacro
2840bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2841 SMLoc DirectiveLoc) {
2842 if (getLexer().isNot(AsmToken::EndOfStatement))
2843 return TokError("unexpected token in '" + Directive + "' directive");
2844
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002845 // If we are inside a macro instantiation, terminate the current
2846 // instantiation.
2847 if (!getParser().ActiveMacros.empty()) {
2848 getParser().HandleMacroExit();
2849 return false;
2850 }
2851
2852 // Otherwise, this .endmacro is a stray entry in the file; well formed
2853 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002854 return TokError("unexpected '" + Directive + "' in file, "
2855 "no current macro definition");
2856}
2857
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002858bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002859 getParser().CheckForValidSection();
2860
2861 const MCExpr *Value;
2862
2863 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002864 return true;
2865
2866 if (getLexer().isNot(AsmToken::EndOfStatement))
2867 return TokError("unexpected token in directive");
2868
2869 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002870 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002871 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002872 getStreamer().EmitULEB128Value(Value);
2873
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002874 return false;
2875}
2876
2877
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002878/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002879MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002880 MCContext &C, MCStreamer &Out,
2881 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00002882 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002883}