blob: 19f7bd8959a2b073469d40d1ce7246eaa9fc5a73 [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"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000028#include "llvm/MC/MCSymbol.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000029#include "llvm/MC/MCDwarf.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000031#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000032#include "llvm/Support/raw_ostream.h"
Roman Divacky54b0f4f2011-01-27 17:16:37 +000033#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000034#include "llvm/Target/TargetAsmParser.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000035#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000036#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000037using namespace llvm;
38
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000039namespace {
40
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000041/// \brief Helper class for tracking macro definitions.
42struct Macro {
43 StringRef Name;
44 StringRef Body;
45
46public:
47 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
48};
49
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000050/// \brief Helper class for storing information about an active macro
51/// instantiation.
52struct MacroInstantiation {
53 /// The macro being instantiated.
54 const Macro *TheMacro;
55
56 /// The macro instantiation with substitutions.
57 MemoryBuffer *Instantiation;
58
59 /// The location of the instantiation.
60 SMLoc InstantiationLoc;
61
62 /// The location where parsing should resume upon instantiation completion.
63 SMLoc ExitLoc;
64
65public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000066 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
67 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000068};
69
Daniel Dunbaraef87e32010-07-18 18:31:38 +000070/// \brief The concrete assembly parser instance.
71class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000072 friend class GenericAsmParser;
73
Daniel Dunbaraef87e32010-07-18 18:31:38 +000074 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
75 void operator=(const AsmParser &); // DO NOT IMPLEMENT
76private:
77 AsmLexer Lexer;
78 MCContext &Ctx;
79 MCStreamer &Out;
80 SourceMgr &SrcMgr;
81 MCAsmParserExtension *GenericParser;
82 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000083
Daniel Dunbaraef87e32010-07-18 18:31:38 +000084 /// This is the current buffer index we're lexing from as managed by the
85 /// SourceMgr object.
86 int CurBuffer;
87
88 AsmCond TheCondState;
89 std::vector<AsmCond> TheCondStack;
90
91 /// DirectiveMap - This is a table handlers for directives. Each handler is
92 /// invoked after the directive identifier is read and is responsible for
93 /// parsing and validating the rest of the directive. The handler is passed
94 /// in the directive name and the location of the directive keyword.
95 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000096
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000097 /// MacroMap - Map of currently defined macros.
98 StringMap<Macro*> MacroMap;
99
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000100 /// ActiveMacros - Stack of active macro instantiations.
101 std::vector<MacroInstantiation*> ActiveMacros;
102
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000103 /// Boolean tracking whether macro substitution is enabled.
104 unsigned MacrosEnabled : 1;
105
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000106 /// Flag tracking whether any errors have been encountered.
107 unsigned HadError : 1;
108
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000109public:
110 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
111 const MCAsmInfo &MAI);
112 ~AsmParser();
113
114 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
115
116 void AddDirectiveHandler(MCAsmParserExtension *Object,
117 StringRef Directive,
118 DirectiveHandler Handler) {
119 DirectiveMap[Directive] = std::make_pair(Object, Handler);
120 }
121
122public:
123 /// @name MCAsmParser Interface
124 /// {
125
126 virtual SourceMgr &getSourceManager() { return SrcMgr; }
127 virtual MCAsmLexer &getLexer() { return Lexer; }
128 virtual MCContext &getContext() { return Ctx; }
129 virtual MCStreamer &getStreamer() { return Out; }
130
131 virtual void Warning(SMLoc L, const Twine &Meg);
132 virtual bool Error(SMLoc L, const Twine &Msg);
133
134 const AsmToken &Lex();
135
136 bool ParseExpression(const MCExpr *&Res);
137 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
138 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
139 virtual bool ParseAbsoluteExpression(int64_t &Res);
140
141 /// }
142
143private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000144 void CheckForValidSection();
145
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000146 bool ParseStatement();
147
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000148 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
149 void HandleMacroExit();
150
151 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000152 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
153 SrcMgr.PrintMessage(Loc, Msg, Type);
154 }
155
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000156 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
157 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000158
159 /// \brief Reset the current lexer position to that given by \arg Loc. The
160 /// current token is not set; clients should ensure Lex() is called
161 /// subsequently.
162 void JumpToLoc(SMLoc Loc);
163
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000164 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000165
166 /// \brief Parse up to the end of statement and a return the contents from the
167 /// current token until the end of the statement; the current token on exit
168 /// will be either the EndOfStatement or EOF.
169 StringRef ParseStringToEndOfStatement();
170
Nico Weber4c4c7322011-01-28 03:04:41 +0000171 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000172
173 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
174 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
175 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000176 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000177
178 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
179 /// and set \arg Res to the identifier contents.
180 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000181
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000182 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000183
184 // ".ascii", ".asciiz", ".string"
185 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000186 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000187 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000188 bool ParseDirectiveFill(); // ".fill"
189 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000190 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000191 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000192 bool ParseDirectiveOrg(); // ".org"
193 // ".align{,32}", ".p2align{,w,l}"
194 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
195
196 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
197 /// accepts a single symbol (which should be a label or an external).
198 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000199
200 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
201
202 bool ParseDirectiveAbort(); // ".abort"
203 bool ParseDirectiveInclude(); // ".include"
204
205 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000206 // ".ifdef" or ".ifndef", depending on expect_defined
207 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000208 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
209 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
210 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
211
212 /// ParseEscapedString - Parse the current token as a string which may include
213 /// escaped characters and return the string contents.
214 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000215
216 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
217 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000218};
219
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000220/// \brief Generic implementations of directive handling, etc. which is shared
221/// (or the default, at least) for all assembler parser.
222class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000223 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
224 void AddDirectiveHandler(StringRef Directive) {
225 getParser().AddDirectiveHandler(this, Directive,
226 HandleDirective<GenericAsmParser, Handler>);
227 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000228public:
229 GenericAsmParser() {}
230
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000231 AsmParser &getParser() {
232 return (AsmParser&) this->MCAsmParserExtension::getParser();
233 }
234
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000235 virtual void Initialize(MCAsmParser &Parser) {
236 // Call the base implementation.
237 this->MCAsmParserExtension::Initialize(Parser);
238
239 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
241 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
242 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000244
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000245 // CFI directives.
246 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
247 ".cfi_startproc");
248 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
249 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000250 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
251 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000252 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
253 ".cfi_def_cfa_offset");
254 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
255 ".cfi_def_cfa_register");
256 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
257 ".cfi_offset");
258 AddDirectiveHandler<
259 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
260 AddDirectiveHandler<
261 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000262 AddDirectiveHandler<
263 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
264 AddDirectiveHandler<
265 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000266
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000267 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
269 ".macros_on");
270 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
271 ".macros_off");
272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
273 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
274 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000275
276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
277 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000278 }
279
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000280 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
281
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000282 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
283 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
284 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000285 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000286 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
287 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000288 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000289 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
290 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
291 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
292 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000293 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
294 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000295
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000296 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000297 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
298 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000299
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000300 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000301};
302
303}
304
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000305namespace llvm {
306
307extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000308extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000309extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000310
311}
312
Chris Lattneraaec2052010-01-19 19:46:13 +0000313enum { DEFAULT_ADDRSPACE = 0 };
314
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000315AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
316 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000317 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000318 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000319 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000320 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000321
322 // Initialize the generic parser.
323 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000324
325 // Initialize the platform / file format parser.
326 //
327 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
328 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000329 if (_MAI.hasMicrosoftFastStdCallMangling()) {
330 PlatformParser = createCOFFAsmParser();
331 PlatformParser->Initialize(*this);
332 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000333 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000334 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000335 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000336 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000337 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000338 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000339}
340
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000341AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000342 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
343
344 // Destroy any macros.
345 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
346 ie = MacroMap.end(); it != ie; ++it)
347 delete it->getValue();
348
Daniel Dunbare4749702010-07-12 18:12:02 +0000349 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000350 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000351}
352
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000353void AsmParser::PrintMacroInstantiations() {
354 // Print the active macro instantiation stack.
355 for (std::vector<MacroInstantiation*>::const_reverse_iterator
356 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
357 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
358 "note");
359}
360
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000361void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000362 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000363 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000364}
365
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000366bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000367 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000368 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000369 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000370 return true;
371}
372
Sean Callananfd0b0282010-01-21 00:19:58 +0000373bool AsmParser::EnterIncludeFile(const std::string &Filename) {
374 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
375 if (NewBuf == -1)
376 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000377
Sean Callananfd0b0282010-01-21 00:19:58 +0000378 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000379
Sean Callananfd0b0282010-01-21 00:19:58 +0000380 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000381
Sean Callananfd0b0282010-01-21 00:19:58 +0000382 return false;
383}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000384
385void AsmParser::JumpToLoc(SMLoc Loc) {
386 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
387 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
388}
389
Sean Callananfd0b0282010-01-21 00:19:58 +0000390const AsmToken &AsmParser::Lex() {
391 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000392
Sean Callananfd0b0282010-01-21 00:19:58 +0000393 if (tok->is(AsmToken::Eof)) {
394 // If this is the end of an included file, pop the parent file off the
395 // include stack.
396 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
397 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000398 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000399 tok = &Lexer.Lex();
400 }
401 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000402
Sean Callananfd0b0282010-01-21 00:19:58 +0000403 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000404 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000405
Sean Callananfd0b0282010-01-21 00:19:58 +0000406 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000407}
408
Chris Lattner79180e22010-04-05 23:15:42 +0000409bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000410 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000411 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000412 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000413
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000414 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000415 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000416
417 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000418 AsmCond StartingCondState = TheCondState;
419
Chris Lattnerb717fb02009-07-02 21:53:43 +0000420 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000421 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000422 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000423
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000424 // We had an error, validate that one was emitted and recover by skipping to
425 // the next line.
426 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000427 EatToEndOfStatement();
428 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000429
430 if (TheCondState.TheCond != StartingCondState.TheCond ||
431 TheCondState.Ignore != StartingCondState.Ignore)
432 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000433
434 // Check to see there are no empty DwarfFile slots.
435 const std::vector<MCDwarfFile *> &MCDwarfFiles =
436 getContext().getMCDwarfFiles();
437 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000438 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000439 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000440 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000441
Chris Lattner79180e22010-04-05 23:15:42 +0000442 // Finalize the output stream if there are no errors and if the client wants
443 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000444 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000445 Out.Finish();
446
Chris Lattnerb717fb02009-07-02 21:53:43 +0000447 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000448}
449
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000450void AsmParser::CheckForValidSection() {
451 if (!getStreamer().getCurrentSection()) {
452 TokError("expected section directive before assembly directive");
453 Out.SwitchSection(Ctx.getMachOSection(
454 "__TEXT", "__text",
455 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
456 0, SectionKind::getText()));
457 }
458}
459
Chris Lattner2cf5f142009-06-22 01:29:09 +0000460/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
461void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000462 while (Lexer.isNot(AsmToken::EndOfStatement) &&
463 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000464 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000465
Chris Lattner2cf5f142009-06-22 01:29:09 +0000466 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000467 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000468 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000469}
470
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000471StringRef AsmParser::ParseStringToEndOfStatement() {
472 const char *Start = getTok().getLoc().getPointer();
473
474 while (Lexer.isNot(AsmToken::EndOfStatement) &&
475 Lexer.isNot(AsmToken::Eof))
476 Lex();
477
478 const char *End = getTok().getLoc().getPointer();
479 return StringRef(Start, End - Start);
480}
Chris Lattnerc4193832009-06-22 05:51:26 +0000481
Chris Lattner74ec1a32009-06-22 06:32:03 +0000482/// ParseParenExpr - Parse a paren expression and return it.
483/// NOTE: This assumes the leading '(' has already been consumed.
484///
485/// parenexpr ::= expr)
486///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000487bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000488 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000489 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000490 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000491 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000492 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000493 return false;
494}
Chris Lattnerc4193832009-06-22 05:51:26 +0000495
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000496/// ParseBracketExpr - Parse a bracket expression and return it.
497/// NOTE: This assumes the leading '[' has already been consumed.
498///
499/// bracketexpr ::= expr]
500///
501bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
502 if (ParseExpression(Res)) return true;
503 if (Lexer.isNot(AsmToken::RBrac))
504 return TokError("expected ']' in brackets expression");
505 EndLoc = Lexer.getLoc();
506 Lex();
507 return false;
508}
509
Chris Lattner74ec1a32009-06-22 06:32:03 +0000510/// ParsePrimaryExpr - Parse a primary expression and return it.
511/// primaryexpr ::= (parenexpr
512/// primaryexpr ::= symbol
513/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000514/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000515/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000516bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000517 switch (Lexer.getKind()) {
518 default:
519 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000520 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000521 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000522 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000523 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000524 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000525 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000526 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000527 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000528 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000529 EndLoc = Lexer.getLoc();
530
531 StringRef Identifier;
532 if (ParseIdentifier(Identifier))
533 return false;
534
Daniel Dunbarfffff912009-10-16 01:34:54 +0000535 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000536 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000537 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000538
539 // Lookup the symbol variant if used.
540 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000541 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000542 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000543 if (Variant == MCSymbolRefExpr::VK_Invalid) {
544 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000545 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000546 }
547 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000548
Daniel Dunbarfffff912009-10-16 01:34:54 +0000549 // If this is an absolute variable reference, substitute it now to preserve
550 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000551 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000552 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000553 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000554
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000555 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000556 return false;
557 }
558
559 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000560 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000561 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000562 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000563 case AsmToken::Integer: {
564 SMLoc Loc = getTok().getLoc();
565 int64_t IntVal = getTok().getIntVal();
566 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000567 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000568 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000569 // Look for 'b' or 'f' following an Integer as a directional label
570 if (Lexer.getKind() == AsmToken::Identifier) {
571 StringRef IDVal = getTok().getString();
572 if (IDVal == "f" || IDVal == "b"){
573 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
574 IDVal == "f" ? 1 : 0);
575 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
576 getContext());
577 if(IDVal == "b" && Sym->isUndefined())
578 return Error(Loc, "invalid reference to undefined symbol");
579 EndLoc = Lexer.getLoc();
580 Lex(); // Eat identifier.
581 }
582 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000583 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000584 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000585 case AsmToken::Real: {
586 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000587 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000588 Res = MCConstantExpr::Create(IntVal, getContext());
589 Lex(); // Eat token.
590 return false;
591 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000592 case AsmToken::Dot: {
593 // This is a '.' reference, which references the current PC. Emit a
594 // temporary label to the streamer and refer to it.
595 MCSymbol *Sym = Ctx.CreateTempSymbol();
596 Out.EmitLabel(Sym);
597 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
598 EndLoc = Lexer.getLoc();
599 Lex(); // Eat identifier.
600 return false;
601 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000602 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000603 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000604 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000605 case AsmToken::LBrac:
606 if (!PlatformParser->HasBracketExpressions())
607 return TokError("brackets expression not supported on this target");
608 Lex(); // Eat the '['.
609 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000610 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000611 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000612 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000613 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000614 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000615 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000616 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000617 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000618 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000619 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000620 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000621 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000622 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000623 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000624 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000625 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000626 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000627 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000628 }
629}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000630
Chris Lattnerb4307b32010-01-15 19:28:38 +0000631bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000632 SMLoc EndLoc;
633 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000634}
635
Daniel Dunbarcceba832010-09-17 02:47:07 +0000636const MCExpr *
637AsmParser::ApplyModifierToExpr(const MCExpr *E,
638 MCSymbolRefExpr::VariantKind Variant) {
639 // Recurse over the given expression, rebuilding it to apply the given variant
640 // if there is exactly one symbol.
641 switch (E->getKind()) {
642 case MCExpr::Target:
643 case MCExpr::Constant:
644 return 0;
645
646 case MCExpr::SymbolRef: {
647 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
648
649 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
650 TokError("invalid variant on expression '" +
651 getTok().getIdentifier() + "' (already modified)");
652 return E;
653 }
654
655 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
656 }
657
658 case MCExpr::Unary: {
659 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
660 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
661 if (!Sub)
662 return 0;
663 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
664 }
665
666 case MCExpr::Binary: {
667 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
668 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
669 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
670
671 if (!LHS && !RHS)
672 return 0;
673
674 if (!LHS) LHS = BE->getLHS();
675 if (!RHS) RHS = BE->getRHS();
676
677 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
678 }
679 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000680
681 assert(0 && "Invalid expression kind!");
682 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000683}
684
Chris Lattner74ec1a32009-06-22 06:32:03 +0000685/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000686///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000687/// expr ::= expr +,- expr -> lowest.
688/// expr ::= expr |,^,&,! expr -> middle.
689/// expr ::= expr *,/,%,<<,>> expr -> highest.
690/// expr ::= primaryexpr
691///
Chris Lattner54482b42010-01-15 19:39:23 +0000692bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000693 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000694 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000695 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
696 return true;
697
Daniel Dunbarcceba832010-09-17 02:47:07 +0000698 // As a special case, we support 'a op b @ modifier' by rewriting the
699 // expression to include the modifier. This is inefficient, but in general we
700 // expect users to use 'a@modifier op b'.
701 if (Lexer.getKind() == AsmToken::At) {
702 Lex();
703
704 if (Lexer.isNot(AsmToken::Identifier))
705 return TokError("unexpected symbol modifier following '@'");
706
707 MCSymbolRefExpr::VariantKind Variant =
708 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
709 if (Variant == MCSymbolRefExpr::VK_Invalid)
710 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
711
712 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
713 if (!ModifiedRes) {
714 return TokError("invalid modifier '" + getTok().getIdentifier() +
715 "' (no symbols present)");
716 return true;
717 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000718
Daniel Dunbarcceba832010-09-17 02:47:07 +0000719 Res = ModifiedRes;
720 Lex();
721 }
722
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000723 // Try to constant fold it up front, if possible.
724 int64_t Value;
725 if (Res->EvaluateAsAbsolute(Value))
726 Res = MCConstantExpr::Create(Value, getContext());
727
728 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000729}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000730
Chris Lattnerb4307b32010-01-15 19:28:38 +0000731bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000732 Res = 0;
733 return ParseParenExpr(Res, EndLoc) ||
734 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000735}
736
Daniel Dunbar475839e2009-06-29 20:37:27 +0000737bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000738 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000739
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000740 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000741 if (ParseExpression(Expr))
742 return true;
743
Daniel Dunbare00b0112009-10-16 01:57:52 +0000744 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000745 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000746
747 return false;
748}
749
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000750static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000751 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000752 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000753 default:
754 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000755
Daniel Dunbarcceba832010-09-17 02:47:07 +0000756 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000757 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000758 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000759 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000760 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000761 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000762 return 1;
763
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000764
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000765 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000766 //
767 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000768 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000769 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000770 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000771 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000772 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000773 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000774 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000775 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000776 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000777
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000778 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000779 case AsmToken::EqualEqual:
780 Kind = MCBinaryExpr::EQ;
781 return 3;
782 case AsmToken::ExclaimEqual:
783 case AsmToken::LessGreater:
784 Kind = MCBinaryExpr::NE;
785 return 3;
786 case AsmToken::Less:
787 Kind = MCBinaryExpr::LT;
788 return 3;
789 case AsmToken::LessEqual:
790 Kind = MCBinaryExpr::LTE;
791 return 3;
792 case AsmToken::Greater:
793 Kind = MCBinaryExpr::GT;
794 return 3;
795 case AsmToken::GreaterEqual:
796 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000797 return 3;
798
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000799 // High Intermediate Precedence: +, -
800 case AsmToken::Plus:
801 Kind = MCBinaryExpr::Add;
802 return 4;
803 case AsmToken::Minus:
804 Kind = MCBinaryExpr::Sub;
805 return 4;
806
Daniel Dunbar475839e2009-06-29 20:37:27 +0000807 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000808 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000809 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000810 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000811 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000812 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000813 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000814 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000815 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000816 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000817 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000818 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000819 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000820 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000821 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000822 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000823 }
824}
825
826
827/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
828/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000829bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
830 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000831 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000832 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000833 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000834
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000835 // If the next token is lower precedence than we are allowed to eat, return
836 // successfully with what we ate already.
837 if (TokPrec < Precedence)
838 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000839
Sean Callanan79ed1a82010-01-19 20:22:31 +0000840 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000841
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000842 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000843 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000844 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000845
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000846 // If BinOp binds less tightly with RHS than the operator after RHS, let
847 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000848 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000849 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000850 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000851 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000852 }
853
Daniel Dunbar475839e2009-06-29 20:37:27 +0000854 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000855 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000856 }
857}
858
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000859
860
861
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000862/// ParseStatement:
863/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000864/// ::= Label* Directive ...Operands... EndOfStatement
865/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000866bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000867 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000868 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000869 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000870 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000871 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000872
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000873 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000874 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000875 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000876 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000877 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000878 // A full line comment is a '#' as the first token.
879 if (Lexer.is(AsmToken::Hash)) {
880 EatToEndOfStatement();
881 return false;
882 }
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000883
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000884 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000885 if (Lexer.is(AsmToken::Integer)) {
886 LocalLabelVal = getTok().getIntVal();
887 if (LocalLabelVal < 0) {
888 if (!TheCondState.Ignore)
889 return TokError("unexpected token at start of statement");
890 IDVal = "";
891 }
892 else {
893 IDVal = getTok().getString();
894 Lex(); // Consume the integer token to be used as an identifier token.
895 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000896 if (!TheCondState.Ignore)
897 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000898 }
899 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000900
901 } else if (Lexer.is(AsmToken::Dot)) {
902 // Treat '.' as a valid identifier in this context.
903 Lex();
904 IDVal = ".";
905
Daniel Dunbar0143ac12011-03-25 17:47:14 +0000906 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000907 if (!TheCondState.Ignore)
908 return TokError("unexpected token at start of statement");
909 IDVal = "";
910 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000911
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000912
Chris Lattner7834fac2010-04-17 18:14:27 +0000913 // Handle conditional assembly here before checking for skipping. We
914 // have to do this so that .endif isn't skipped in a ".if 0" block for
915 // example.
916 if (IDVal == ".if")
917 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000918 if (IDVal == ".ifdef")
919 return ParseDirectiveIfdef(IDLoc, true);
920 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
921 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000922 if (IDVal == ".elseif")
923 return ParseDirectiveElseIf(IDLoc);
924 if (IDVal == ".else")
925 return ParseDirectiveElse(IDLoc);
926 if (IDVal == ".endif")
927 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000928
Chris Lattner7834fac2010-04-17 18:14:27 +0000929 // If we are in a ".if 0" block, ignore this statement.
930 if (TheCondState.Ignore) {
931 EatToEndOfStatement();
932 return false;
933 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000934
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000935 // FIXME: Recurse on local labels?
936
937 // See what kind of statement we have.
938 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000939 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000940 CheckForValidSection();
941
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000942 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000943 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000944
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000945 // Diagnose attempt to use '.' as a label.
946 if (IDVal == ".")
947 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
948
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000949 // Diagnose attempt to use a variable as a label.
950 //
951 // FIXME: Diagnostics. Note the location of the definition as a label.
952 // FIXME: This doesn't diagnose assignment to a symbol which has been
953 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000954 MCSymbol *Sym;
955 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000956 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000957 else
958 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000959 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000960 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000961
Daniel Dunbar959fd882009-08-26 22:13:22 +0000962 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000963 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000964
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000965 // Consume any end of statement token, if present, to avoid spurious
966 // AddBlankLine calls().
967 if (Lexer.is(AsmToken::EndOfStatement)) {
968 Lex();
969 if (Lexer.is(AsmToken::Eof))
970 return false;
971 }
972
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000973 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000974 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000975
Daniel Dunbar3f872332009-07-28 16:08:33 +0000976 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000977 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000978 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000979
Nico Weber4c4c7322011-01-28 03:04:41 +0000980 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000981
982 default: // Normal instruction or directive.
983 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000984 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000985
986 // If macros are enabled, check to see if this is a macro instantiation.
987 if (MacrosEnabled)
988 if (const Macro *M = MacroMap.lookup(IDVal))
989 return HandleMacroEntry(IDVal, IDLoc, M);
990
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000991 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +0000992 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000993 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +0000994 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +0000995 return ParseDirectiveSet(IDVal, true);
996 if (IDVal == ".equiv")
997 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000998
Daniel Dunbara0d14262009-06-24 23:30:00 +0000999 // Data directives
1000
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001001 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001002 return ParseDirectiveAscii(IDVal, false);
1003 if (IDVal == ".asciz" || IDVal == ".string")
1004 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001005
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001006 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001007 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001008 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001009 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001010 if (IDVal == ".value")
1011 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001012 if (IDVal == ".2byte")
1013 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001014 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001015 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001016 if (IDVal == ".int")
1017 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001018 if (IDVal == ".4byte")
1019 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001020 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001022 if (IDVal == ".8byte")
1023 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001024 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001025 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1026 if (IDVal == ".double")
1027 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001028
Eli Friedman5d68ec22010-07-19 04:17:25 +00001029 if (IDVal == ".align") {
1030 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1031 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1032 }
1033 if (IDVal == ".align32") {
1034 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1035 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1036 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001037 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001038 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001039 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001040 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001041 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001042 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001043 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001044 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001045 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001046 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001047 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001048 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1049
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001050 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001051 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001052
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001053 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001054 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001055 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001056 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001057 if (IDVal == ".zero")
1058 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001059
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001060 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001061
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001062 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001063 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001064 // ELF only? Should it be here?
1065 if (IDVal == ".local")
1066 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001067 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001068 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001069 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001070 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001071 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001072 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001073 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001074 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001075 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001076 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001077 if (IDVal == ".symbol_resolver")
1078 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001079 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001080 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001081 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001082 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001083 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001084 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001085 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001086 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001087 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001088 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001089 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001090 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001091 if (IDVal == ".weak_def_can_be_hidden")
1092 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001093
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001094 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001095 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001096 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001097 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001098
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001099 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001100 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001101 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001102 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001103
Roman Divackybb6d14f2011-01-31 21:19:43 +00001104 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001105 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001106
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001107 // Look up the handler in the handler table.
1108 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1109 DirectiveMap.lookup(IDVal);
1110 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001111 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001112
Kevin Enderby9c656452009-09-10 20:51:44 +00001113 // Target hook for parsing target specific directives.
1114 if (!getTargetParser().ParseDirective(ID))
1115 return false;
1116
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001117 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001118 EatToEndOfStatement();
1119 return false;
1120 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001121
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001122 CheckForValidSection();
1123
Chris Lattnera7f13542010-05-19 23:34:33 +00001124 // Canonicalize the opcode to lower case.
1125 SmallString<128> Opcode;
1126 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1127 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001128
Chris Lattner98986712010-01-14 22:21:20 +00001129 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001130 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001131 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001132
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001133 // Dump the parsed representation, if requested.
1134 if (getShowParsedOperands()) {
1135 SmallString<256> Str;
1136 raw_svector_ostream OS(Str);
1137 OS << "parsed instruction: [";
1138 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1139 if (i != 0)
1140 OS << ", ";
1141 ParsedOperands[i]->dump(OS);
1142 }
1143 OS << "]";
1144
1145 PrintMessage(IDLoc, OS.str(), "note");
1146 }
1147
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001148 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001149 if (!HadError)
1150 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1151 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001152
Chris Lattner98986712010-01-14 22:21:20 +00001153 // Free any parsed operands.
1154 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1155 delete ParsedOperands[i];
1156
Chris Lattnercbf8a982010-09-11 16:18:25 +00001157 // Don't skip the rest of the line, the instruction parser is responsible for
1158 // that.
1159 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001160}
Chris Lattner9a023f72009-06-24 04:43:34 +00001161
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001162MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1163 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001164 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1165{
1166 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1167 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001168 SmallString<256> Buf;
1169 raw_svector_ostream OS(Buf);
1170
1171 StringRef Body = M->Body;
1172 while (!Body.empty()) {
1173 // Scan for the next substitution.
1174 std::size_t End = Body.size(), Pos = 0;
1175 for (; Pos != End; ++Pos) {
1176 // Check for a substitution or escape.
1177 if (Body[Pos] != '$' || Pos + 1 == End)
1178 continue;
1179
1180 char Next = Body[Pos + 1];
1181 if (Next == '$' || Next == 'n' || isdigit(Next))
1182 break;
1183 }
1184
1185 // Add the prefix.
1186 OS << Body.slice(0, Pos);
1187
1188 // Check if we reached the end.
1189 if (Pos == End)
1190 break;
1191
1192 switch (Body[Pos+1]) {
1193 // $$ => $
1194 case '$':
1195 OS << '$';
1196 break;
1197
1198 // $n => number of arguments
1199 case 'n':
1200 OS << A.size();
1201 break;
1202
1203 // $[0-9] => argument
1204 default: {
1205 // Missing arguments are ignored.
1206 unsigned Index = Body[Pos+1] - '0';
1207 if (Index >= A.size())
1208 break;
1209
1210 // Otherwise substitute with the token values, with spaces eliminated.
1211 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1212 ie = A[Index].end(); it != ie; ++it)
1213 OS << it->getString();
1214 break;
1215 }
1216 }
1217
1218 // Update the scan point.
1219 Body = Body.substr(Pos + 2);
1220 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001221
1222 // We include the .endmacro in the buffer as our queue to exit the macro
1223 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001224 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001225
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001226 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001227}
1228
1229bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1230 const Macro *M) {
1231 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1232 // this, although we should protect against infinite loops.
1233 if (ActiveMacros.size() == 20)
1234 return TokError("macros cannot be nested more than 20 levels deep");
1235
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001236 // Parse the macro instantiation arguments.
1237 std::vector<std::vector<AsmToken> > MacroArguments;
1238 MacroArguments.push_back(std::vector<AsmToken>());
1239 unsigned ParenLevel = 0;
1240 for (;;) {
1241 if (Lexer.is(AsmToken::Eof))
1242 return TokError("unexpected token in macro instantiation");
1243 if (Lexer.is(AsmToken::EndOfStatement))
1244 break;
1245
1246 // If we aren't inside parentheses and this is a comma, start a new token
1247 // list.
1248 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1249 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001250 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001251 // Adjust the current parentheses level.
1252 if (Lexer.is(AsmToken::LParen))
1253 ++ParenLevel;
1254 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1255 --ParenLevel;
1256
1257 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001258 MacroArguments.back().push_back(getTok());
1259 }
1260 Lex();
1261 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001262
1263 // Create the macro instantiation object and add to the current macro
1264 // instantiation stack.
1265 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001266 getTok().getLoc(),
1267 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001268 ActiveMacros.push_back(MI);
1269
1270 // Jump to the macro instantiation and prime the lexer.
1271 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1272 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1273 Lex();
1274
1275 return false;
1276}
1277
1278void AsmParser::HandleMacroExit() {
1279 // Jump to the EndOfStatement we should return to, and consume it.
1280 JumpToLoc(ActiveMacros.back()->ExitLoc);
1281 Lex();
1282
1283 // Pop the instantiation entry.
1284 delete ActiveMacros.back();
1285 ActiveMacros.pop_back();
1286}
1287
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001288static void MarkUsed(const MCExpr *Value) {
1289 switch (Value->getKind()) {
1290 case MCExpr::Binary:
1291 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1292 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1293 break;
1294 case MCExpr::Target:
1295 case MCExpr::Constant:
1296 break;
1297 case MCExpr::SymbolRef: {
1298 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1299 break;
1300 }
1301 case MCExpr::Unary:
1302 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1303 break;
1304 }
1305}
1306
Nico Weber4c4c7322011-01-28 03:04:41 +00001307bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001308 // FIXME: Use better location, we should use proper tokens.
1309 SMLoc EqualLoc = Lexer.getLoc();
1310
Daniel Dunbar821e3332009-08-31 08:09:28 +00001311 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001312 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001313 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001314
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001315 MarkUsed(Value);
1316
Daniel Dunbar3f872332009-07-28 16:08:33 +00001317 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001318 return TokError("unexpected token in assignment");
1319
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001320 // Error on assignment to '.'.
1321 if (Name == ".") {
1322 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1323 "(use '.space' or '.org').)"));
1324 }
1325
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001326 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001327 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001328
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001329 // Validate that the LHS is allowed to be a variable (either it has not been
1330 // used as a symbol, or it is an absolute symbol).
1331 MCSymbol *Sym = getContext().LookupSymbol(Name);
1332 if (Sym) {
1333 // Diagnose assignment to a label.
1334 //
1335 // FIXME: Diagnostics. Note the location of the definition as a label.
1336 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001337 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001338 ; // Allow redefinitions of undefined symbols only used in directives.
Nico Weber4c4c7322011-01-28 03:04:41 +00001339 else if (!Sym->isUndefined() && (!Sym->isAbsolute() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001340 return Error(EqualLoc, "redefinition of '" + Name + "'");
1341 else if (!Sym->isVariable())
1342 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001343 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001344 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1345 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001346
1347 // Don't count these checks as uses.
1348 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001349 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001350 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001351
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001352 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001353
1354 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001355 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001356
1357 return false;
1358}
1359
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001360/// ParseIdentifier:
1361/// ::= identifier
1362/// ::= string
1363bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001364 // The assembler has relaxed rules for accepting identifiers, in particular we
1365 // allow things like '.globl $foo', which would normally be separate
1366 // tokens. At this level, we have already lexed so we cannot (currently)
1367 // handle this as a context dependent token, instead we detect adjacent tokens
1368 // and return the combined identifier.
1369 if (Lexer.is(AsmToken::Dollar)) {
1370 SMLoc DollarLoc = getLexer().getLoc();
1371
1372 // Consume the dollar sign, and check for a following identifier.
1373 Lex();
1374 if (Lexer.isNot(AsmToken::Identifier))
1375 return true;
1376
1377 // We have a '$' followed by an identifier, make sure they are adjacent.
1378 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1379 return true;
1380
1381 // Construct the joined identifier and consume the token.
1382 Res = StringRef(DollarLoc.getPointer(),
1383 getTok().getIdentifier().size() + 1);
1384 Lex();
1385 return false;
1386 }
1387
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001388 if (Lexer.isNot(AsmToken::Identifier) &&
1389 Lexer.isNot(AsmToken::String))
1390 return true;
1391
Sean Callanan18b83232010-01-19 21:44:56 +00001392 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001393
Sean Callanan79ed1a82010-01-19 20:22:31 +00001394 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001395
1396 return false;
1397}
1398
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001399/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001400/// ::= .equ identifier ',' expression
1401/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001402/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001403bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001404 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001405
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001406 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001407 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001408
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001409 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001410 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001411 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001412
Nico Weber4c4c7322011-01-28 03:04:41 +00001413 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001414}
1415
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001416bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001417 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001418
1419 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001420 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001421 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1422 if (Str[i] != '\\') {
1423 Data += Str[i];
1424 continue;
1425 }
1426
1427 // Recognize escaped characters. Note that this escape semantics currently
1428 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1429 ++i;
1430 if (i == e)
1431 return TokError("unexpected backslash at end of string");
1432
1433 // Recognize octal sequences.
1434 if ((unsigned) (Str[i] - '0') <= 7) {
1435 // Consume up to three octal characters.
1436 unsigned Value = Str[i] - '0';
1437
1438 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1439 ++i;
1440 Value = Value * 8 + (Str[i] - '0');
1441
1442 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1443 ++i;
1444 Value = Value * 8 + (Str[i] - '0');
1445 }
1446 }
1447
1448 if (Value > 255)
1449 return TokError("invalid octal escape sequence (out of range)");
1450
1451 Data += (unsigned char) Value;
1452 continue;
1453 }
1454
1455 // Otherwise recognize individual escapes.
1456 switch (Str[i]) {
1457 default:
1458 // Just reject invalid escape sequences for now.
1459 return TokError("invalid escape sequence (unrecognized character)");
1460
1461 case 'b': Data += '\b'; break;
1462 case 'f': Data += '\f'; break;
1463 case 'n': Data += '\n'; break;
1464 case 'r': Data += '\r'; break;
1465 case 't': Data += '\t'; break;
1466 case '"': Data += '"'; break;
1467 case '\\': Data += '\\'; break;
1468 }
1469 }
1470
1471 return false;
1472}
1473
Daniel Dunbara0d14262009-06-24 23:30:00 +00001474/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001475/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1476bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001477 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001478 CheckForValidSection();
1479
Daniel Dunbara0d14262009-06-24 23:30:00 +00001480 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001481 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001482 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001483
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001484 std::string Data;
1485 if (ParseEscapedString(Data))
1486 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001487
1488 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001489 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001490 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1491
Sean Callanan79ed1a82010-01-19 20:22:31 +00001492 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001493
1494 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001495 break;
1496
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001497 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001498 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001499 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001500 }
1501 }
1502
Sean Callanan79ed1a82010-01-19 20:22:31 +00001503 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001504 return false;
1505}
1506
1507/// ParseDirectiveValue
1508/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1509bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001510 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001511 CheckForValidSection();
1512
Daniel Dunbara0d14262009-06-24 23:30:00 +00001513 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001514 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001515 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001516 return true;
1517
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001518 // Special case constant expressions to match code generator.
1519 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001520 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001521 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001522 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001523
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001524 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001525 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001526
Daniel Dunbara0d14262009-06-24 23:30:00 +00001527 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001528 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001529 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001530 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001531 }
1532 }
1533
Sean Callanan79ed1a82010-01-19 20:22:31 +00001534 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001535 return false;
1536}
1537
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001538/// ParseDirectiveRealValue
1539/// ::= (.single | .double) [ expression (, expression)* ]
1540bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1541 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1542 CheckForValidSection();
1543
1544 for (;;) {
1545 // We don't truly support arithmetic on floating point expressions, so we
1546 // have to manually parse unary prefixes.
1547 bool IsNeg = false;
1548 if (getLexer().is(AsmToken::Minus)) {
1549 Lex();
1550 IsNeg = true;
1551 } else if (getLexer().is(AsmToken::Plus))
1552 Lex();
1553
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001554 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001555 getLexer().isNot(AsmToken::Real))
1556 return TokError("unexpected token in directive");
1557
1558 // Convert to an APFloat.
1559 APFloat Value(Semantics);
1560 if (Value.convertFromString(getTok().getString(),
1561 APFloat::rmNearestTiesToEven) ==
1562 APFloat::opInvalidOp)
1563 return TokError("invalid floating point literal");
1564 if (IsNeg)
1565 Value.changeSign();
1566
1567 // Consume the numeric token.
1568 Lex();
1569
1570 // Emit the value as an integer.
1571 APInt AsInt = Value.bitcastToAPInt();
1572 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1573 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1574
1575 if (getLexer().is(AsmToken::EndOfStatement))
1576 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001577
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001578 if (getLexer().isNot(AsmToken::Comma))
1579 return TokError("unexpected token in directive");
1580 Lex();
1581 }
1582 }
1583
1584 Lex();
1585 return false;
1586}
1587
Daniel Dunbara0d14262009-06-24 23:30:00 +00001588/// ParseDirectiveSpace
1589/// ::= .space expression [ , expression ]
1590bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001591 CheckForValidSection();
1592
Daniel Dunbara0d14262009-06-24 23:30:00 +00001593 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001594 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001595 return true;
1596
1597 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001598 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1599 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001600 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001601 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001602
Daniel Dunbar475839e2009-06-29 20:37:27 +00001603 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001604 return true;
1605
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001606 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001607 return TokError("unexpected token in '.space' directive");
1608 }
1609
Sean Callanan79ed1a82010-01-19 20:22:31 +00001610 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001611
1612 if (NumBytes <= 0)
1613 return TokError("invalid number of bytes in '.space' directive");
1614
1615 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001616 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001617
1618 return false;
1619}
1620
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001621/// ParseDirectiveZero
1622/// ::= .zero expression
1623bool AsmParser::ParseDirectiveZero() {
1624 CheckForValidSection();
1625
1626 int64_t NumBytes;
1627 if (ParseAbsoluteExpression(NumBytes))
1628 return true;
1629
Rafael Espindolae452b172010-10-05 19:42:57 +00001630 int64_t Val = 0;
1631 if (getLexer().is(AsmToken::Comma)) {
1632 Lex();
1633 if (ParseAbsoluteExpression(Val))
1634 return true;
1635 }
1636
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001637 if (getLexer().isNot(AsmToken::EndOfStatement))
1638 return TokError("unexpected token in '.zero' directive");
1639
1640 Lex();
1641
Rafael Espindolae452b172010-10-05 19:42:57 +00001642 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001643
1644 return false;
1645}
1646
Daniel Dunbara0d14262009-06-24 23:30:00 +00001647/// ParseDirectiveFill
1648/// ::= .fill expression , expression , expression
1649bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001650 CheckForValidSection();
1651
Daniel Dunbara0d14262009-06-24 23:30:00 +00001652 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001653 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001654 return true;
1655
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001656 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001657 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001658 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001659
Daniel Dunbara0d14262009-06-24 23:30:00 +00001660 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001661 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001662 return true;
1663
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001664 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001665 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001666 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001667
Daniel Dunbara0d14262009-06-24 23:30:00 +00001668 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001669 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001670 return true;
1671
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001672 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001673 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001674
Sean Callanan79ed1a82010-01-19 20:22:31 +00001675 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001676
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001677 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1678 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001679
1680 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001681 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001682
1683 return false;
1684}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001685
1686/// ParseDirectiveOrg
1687/// ::= .org expression [ , expression ]
1688bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001689 CheckForValidSection();
1690
Daniel Dunbar821e3332009-08-31 08:09:28 +00001691 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001692 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001693 return true;
1694
1695 // Parse optional fill expression.
1696 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001697 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1698 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001699 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001700 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001701
Daniel Dunbar475839e2009-06-29 20:37:27 +00001702 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001703 return true;
1704
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001705 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001706 return TokError("unexpected token in '.org' directive");
1707 }
1708
Sean Callanan79ed1a82010-01-19 20:22:31 +00001709 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001710
1711 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1712 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001713 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001714
1715 return false;
1716}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001717
1718/// ParseDirectiveAlign
1719/// ::= {.align, ...} expression [ , expression [ , expression ]]
1720bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001721 CheckForValidSection();
1722
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001723 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001724 int64_t Alignment;
1725 if (ParseAbsoluteExpression(Alignment))
1726 return true;
1727
1728 SMLoc MaxBytesLoc;
1729 bool HasFillExpr = false;
1730 int64_t FillExpr = 0;
1731 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001732 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1733 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001734 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001735 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001736
1737 // The fill expression can be omitted while specifying a maximum number of
1738 // alignment bytes, e.g:
1739 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001740 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001741 HasFillExpr = true;
1742 if (ParseAbsoluteExpression(FillExpr))
1743 return true;
1744 }
1745
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001746 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1747 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001748 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001749 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001750
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001751 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001752 if (ParseAbsoluteExpression(MaxBytesToFill))
1753 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001754
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001755 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001756 return TokError("unexpected token in directive");
1757 }
1758 }
1759
Sean Callanan79ed1a82010-01-19 20:22:31 +00001760 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001761
Daniel Dunbar648ac512010-05-17 21:54:30 +00001762 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001763 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001764
1765 // Compute alignment in bytes.
1766 if (IsPow2) {
1767 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001768 if (Alignment >= 32) {
1769 Error(AlignmentLoc, "invalid alignment value");
1770 Alignment = 31;
1771 }
1772
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001773 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001774 }
1775
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001776 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001777 if (MaxBytesLoc.isValid()) {
1778 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001779 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1780 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001781 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001782 }
1783
1784 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001785 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1786 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001787 MaxBytesToFill = 0;
1788 }
1789 }
1790
Daniel Dunbar648ac512010-05-17 21:54:30 +00001791 // Check whether we should use optimal code alignment for this .align
1792 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001793 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001794 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1795 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001796 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001797 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001798 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001799 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1800 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001801 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001802
1803 return false;
1804}
1805
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001806/// ParseDirectiveSymbolAttribute
1807/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001808bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001809 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001810 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001811 StringRef Name;
1812
1813 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001814 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001815
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001816 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001817
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001818 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001819
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001820 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001821 break;
1822
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001823 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001824 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001825 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001826 }
1827 }
1828
Sean Callanan79ed1a82010-01-19 20:22:31 +00001829 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001830 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001831}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001832
1833/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001834/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1835bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001836 CheckForValidSection();
1837
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001838 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001839 StringRef Name;
1840 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001841 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001842
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001843 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001844 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001845
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001846 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001847 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001848 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001849
1850 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001851 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001852 if (ParseAbsoluteExpression(Size))
1853 return true;
1854
1855 int64_t Pow2Alignment = 0;
1856 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001857 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001858 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001859 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001860 if (ParseAbsoluteExpression(Pow2Alignment))
1861 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001862
Chris Lattner258281d2010-01-19 06:22:22 +00001863 // If this target takes alignments in bytes (not log) validate and convert.
1864 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1865 if (!isPowerOf2_64(Pow2Alignment))
1866 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1867 Pow2Alignment = Log2_64(Pow2Alignment);
1868 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001869 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001870
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001871 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001872 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001873
Sean Callanan79ed1a82010-01-19 20:22:31 +00001874 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001875
Chris Lattner1fc3d752009-07-09 17:25:12 +00001876 // NOTE: a size of zero for a .comm should create a undefined symbol
1877 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001878 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001879 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1880 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001881
Eric Christopherc260a3e2010-05-14 01:38:54 +00001882 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001883 // may internally end up wanting an alignment in bytes.
1884 // FIXME: Diagnose overflow.
1885 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001886 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1887 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001888
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001889 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001890 return Error(IDLoc, "invalid symbol redefinition");
1891
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001892 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001893 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001894 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001895 getStreamer().EmitZerofill(Ctx.getMachOSection(
1896 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1897 0, SectionKind::getBSS()),
1898 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001899 return false;
1900 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001901
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001902 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001903 return false;
1904}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001905
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001906/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001907/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001908bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001909 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001910 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001911
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001912 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001913 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001914 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001915
Sean Callanan79ed1a82010-01-19 20:22:31 +00001916 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001917
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001918 if (Str.empty())
1919 Error(Loc, ".abort detected. Assembly stopping.");
1920 else
1921 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001922 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001923
1924 return false;
1925}
Kevin Enderby71148242009-07-14 21:35:03 +00001926
Kevin Enderby1f049b22009-07-14 23:21:55 +00001927/// ParseDirectiveInclude
1928/// ::= .include "filename"
1929bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001930 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001931 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001932
Sean Callanan18b83232010-01-19 21:44:56 +00001933 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001934 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001935 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001936
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001937 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001938 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001939
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001940 // Strip the quotes.
1941 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001942
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001943 // Attempt to switch the lexer to the included file before consuming the end
1944 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001945 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001946 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001947 return true;
1948 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001949
1950 return false;
1951}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001952
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001953/// ParseDirectiveIf
1954/// ::= .if expression
1955bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001956 TheCondStack.push_back(TheCondState);
1957 TheCondState.TheCond = AsmCond::IfCond;
1958 if(TheCondState.Ignore) {
1959 EatToEndOfStatement();
1960 }
1961 else {
1962 int64_t ExprValue;
1963 if (ParseAbsoluteExpression(ExprValue))
1964 return true;
1965
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001966 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001967 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001968
Sean Callanan79ed1a82010-01-19 20:22:31 +00001969 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001970
1971 TheCondState.CondMet = ExprValue;
1972 TheCondState.Ignore = !TheCondState.CondMet;
1973 }
1974
1975 return false;
1976}
1977
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001978bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
1979 StringRef Name;
1980 TheCondStack.push_back(TheCondState);
1981 TheCondState.TheCond = AsmCond::IfCond;
1982
1983 if (TheCondState.Ignore) {
1984 EatToEndOfStatement();
1985 } else {
1986 if (ParseIdentifier(Name))
1987 return TokError("expected identifier after '.ifdef'");
1988
1989 Lex();
1990
1991 MCSymbol *Sym = getContext().LookupSymbol(Name);
1992
1993 if (expect_defined)
1994 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
1995 else
1996 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
1997 TheCondState.Ignore = !TheCondState.CondMet;
1998 }
1999
2000 return false;
2001}
2002
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002003/// ParseDirectiveElseIf
2004/// ::= .elseif expression
2005bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2006 if (TheCondState.TheCond != AsmCond::IfCond &&
2007 TheCondState.TheCond != AsmCond::ElseIfCond)
2008 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2009 " an .elseif");
2010 TheCondState.TheCond = AsmCond::ElseIfCond;
2011
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002012 bool LastIgnoreState = false;
2013 if (!TheCondStack.empty())
2014 LastIgnoreState = TheCondStack.back().Ignore;
2015 if (LastIgnoreState || TheCondState.CondMet) {
2016 TheCondState.Ignore = true;
2017 EatToEndOfStatement();
2018 }
2019 else {
2020 int64_t ExprValue;
2021 if (ParseAbsoluteExpression(ExprValue))
2022 return true;
2023
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002024 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002025 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002026
Sean Callanan79ed1a82010-01-19 20:22:31 +00002027 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002028 TheCondState.CondMet = ExprValue;
2029 TheCondState.Ignore = !TheCondState.CondMet;
2030 }
2031
2032 return false;
2033}
2034
2035/// ParseDirectiveElse
2036/// ::= .else
2037bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002038 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002039 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002040
Sean Callanan79ed1a82010-01-19 20:22:31 +00002041 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002042
2043 if (TheCondState.TheCond != AsmCond::IfCond &&
2044 TheCondState.TheCond != AsmCond::ElseIfCond)
2045 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2046 ".elseif");
2047 TheCondState.TheCond = AsmCond::ElseCond;
2048 bool LastIgnoreState = false;
2049 if (!TheCondStack.empty())
2050 LastIgnoreState = TheCondStack.back().Ignore;
2051 if (LastIgnoreState || TheCondState.CondMet)
2052 TheCondState.Ignore = true;
2053 else
2054 TheCondState.Ignore = false;
2055
2056 return false;
2057}
2058
2059/// ParseDirectiveEndIf
2060/// ::= .endif
2061bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002062 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002063 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002064
Sean Callanan79ed1a82010-01-19 20:22:31 +00002065 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002066
2067 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2068 TheCondStack.empty())
2069 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2070 ".else");
2071 if (!TheCondStack.empty()) {
2072 TheCondState = TheCondStack.back();
2073 TheCondStack.pop_back();
2074 }
2075
2076 return false;
2077}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002078
2079/// ParseDirectiveFile
2080/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002081bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002082 // FIXME: I'm not sure what this is.
2083 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002084 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002085 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002086 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002087 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002088
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002089 if (FileNumber < 1)
2090 return TokError("file number less than one");
2091 }
2092
Daniel Dunbareceec052010-07-12 17:45:27 +00002093 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002094 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002095
Chris Lattnerd32e8032010-01-25 19:02:58 +00002096 StringRef Filename = getTok().getString();
2097 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002098 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002099
Daniel Dunbareceec052010-07-12 17:45:27 +00002100 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002101 return TokError("unexpected token in '.file' directive");
2102
Chris Lattnerd32e8032010-01-25 19:02:58 +00002103 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002104 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002105 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002106 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002107 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002108 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002109
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002110 return false;
2111}
2112
2113/// ParseDirectiveLine
2114/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002115bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002116 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2117 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002118 return TokError("unexpected token in '.line' directive");
2119
Sean Callanan18b83232010-01-19 21:44:56 +00002120 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002121 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002122 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002123
2124 // FIXME: Do something with the .line.
2125 }
2126
Daniel Dunbareceec052010-07-12 17:45:27 +00002127 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002128 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002129
2130 return false;
2131}
2132
2133
2134/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002135/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002136/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2137/// The first number is a file number, must have been previously assigned with
2138/// a .file directive, the second number is the line number and optionally the
2139/// third number is a column position (zero if not specified). The remaining
2140/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002141bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002142
Daniel Dunbareceec052010-07-12 17:45:27 +00002143 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002144 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002145 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002146 if (FileNumber < 1)
2147 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002148 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002149 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002150 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002151
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002152 int64_t LineNumber = 0;
2153 if (getLexer().is(AsmToken::Integer)) {
2154 LineNumber = getTok().getIntVal();
2155 if (LineNumber < 1)
2156 return TokError("line number less than one in '.loc' directive");
2157 Lex();
2158 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002159
2160 int64_t ColumnPos = 0;
2161 if (getLexer().is(AsmToken::Integer)) {
2162 ColumnPos = getTok().getIntVal();
2163 if (ColumnPos < 0)
2164 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002165 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002166 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002167
Kevin Enderbyc0957932010-09-30 16:52:03 +00002168 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002169 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002170 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002171 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2172 for (;;) {
2173 if (getLexer().is(AsmToken::EndOfStatement))
2174 break;
2175
2176 StringRef Name;
2177 SMLoc Loc = getTok().getLoc();
2178 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002179 return TokError("unexpected token in '.loc' directive");
2180
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002181 if (Name == "basic_block")
2182 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2183 else if (Name == "prologue_end")
2184 Flags |= DWARF2_FLAG_PROLOGUE_END;
2185 else if (Name == "epilogue_begin")
2186 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2187 else if (Name == "is_stmt") {
2188 SMLoc Loc = getTok().getLoc();
2189 const MCExpr *Value;
2190 if (getParser().ParseExpression(Value))
2191 return true;
2192 // The expression must be the constant 0 or 1.
2193 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2194 int Value = MCE->getValue();
2195 if (Value == 0)
2196 Flags &= ~DWARF2_FLAG_IS_STMT;
2197 else if (Value == 1)
2198 Flags |= DWARF2_FLAG_IS_STMT;
2199 else
2200 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002201 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002202 else {
2203 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2204 }
2205 }
2206 else if (Name == "isa") {
2207 SMLoc Loc = getTok().getLoc();
2208 const MCExpr *Value;
2209 if (getParser().ParseExpression(Value))
2210 return true;
2211 // The expression must be a constant greater or equal to 0.
2212 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2213 int Value = MCE->getValue();
2214 if (Value < 0)
2215 return Error(Loc, "isa number less than zero");
2216 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002217 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002218 else {
2219 return Error(Loc, "isa number not a constant value");
2220 }
2221 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002222 else if (Name == "discriminator") {
2223 if (getParser().ParseAbsoluteExpression(Discriminator))
2224 return true;
2225 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002226 else {
2227 return Error(Loc, "unknown sub-directive in '.loc' directive");
2228 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002229
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002230 if (getLexer().is(AsmToken::EndOfStatement))
2231 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002232 }
2233 }
2234
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002235 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2236 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002237
2238 return false;
2239}
2240
Daniel Dunbar138abae2010-10-16 04:56:42 +00002241/// ParseDirectiveStabs
2242/// ::= .stabs string, number, number, number
2243bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2244 SMLoc DirectiveLoc) {
2245 return TokError("unsupported directive '" + Directive + "'");
2246}
2247
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002248/// ParseDirectiveCFIStartProc
2249/// ::= .cfi_startproc
2250bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2251 SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002252 return getStreamer().EmitCFIStartProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002253}
2254
2255/// ParseDirectiveCFIEndProc
2256/// ::= .cfi_endproc
2257bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002258 return getStreamer().EmitCFIEndProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002259}
2260
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002261/// ParseRegisterOrRegisterNumber - parse register name or number.
2262bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2263 SMLoc DirectiveLoc) {
2264 unsigned RegNo;
2265
2266 if (getLexer().is(AsmToken::Percent)) {
2267 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2268 DirectiveLoc))
2269 return true;
2270 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2271 } else
2272 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002273
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002274 return false;
2275}
2276
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002277/// ParseDirectiveCFIDefCfa
2278/// ::= .cfi_def_cfa register, offset
2279bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2280 SMLoc DirectiveLoc) {
2281 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002282 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002283 return true;
2284
2285 if (getLexer().isNot(AsmToken::Comma))
2286 return TokError("unexpected token in directive");
2287 Lex();
2288
2289 int64_t Offset = 0;
2290 if (getParser().ParseAbsoluteExpression(Offset))
2291 return true;
2292
2293 return getStreamer().EmitCFIDefCfa(Register, Offset);
2294}
2295
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002296/// ParseDirectiveCFIDefCfaOffset
2297/// ::= .cfi_def_cfa_offset offset
2298bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2299 SMLoc DirectiveLoc) {
2300 int64_t Offset = 0;
2301 if (getParser().ParseAbsoluteExpression(Offset))
2302 return true;
2303
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002304 return getStreamer().EmitCFIDefCfaOffset(Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002305}
2306
2307/// ParseDirectiveCFIDefCfaRegister
2308/// ::= .cfi_def_cfa_register register
2309bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2310 SMLoc DirectiveLoc) {
2311 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002312 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002313 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002314
2315 return getStreamer().EmitCFIDefCfaRegister(Register);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002316}
2317
2318/// ParseDirectiveCFIOffset
2319/// ::= .cfi_off register, offset
2320bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2321 int64_t Register = 0;
2322 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002323
2324 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002325 return true;
2326
2327 if (getLexer().isNot(AsmToken::Comma))
2328 return TokError("unexpected token in directive");
2329 Lex();
2330
2331 if (getParser().ParseAbsoluteExpression(Offset))
2332 return true;
2333
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002334 return getStreamer().EmitCFIOffset(Register, Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002335}
2336
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002337static bool isValidEncoding(int64_t Encoding) {
2338 if (Encoding & ~0xff)
2339 return false;
2340
2341 if (Encoding == dwarf::DW_EH_PE_omit)
2342 return true;
2343
2344 const unsigned Format = Encoding & 0xf;
2345 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2346 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2347 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2348 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2349 return false;
2350
Rafael Espindolacaf11582010-12-29 04:31:26 +00002351 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002352 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002353 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002354 return false;
2355
2356 return true;
2357}
2358
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002359/// ParseDirectiveCFIPersonalityOrLsda
2360/// ::= .cfi_personality encoding, [symbol_name]
2361/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002362bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002363 SMLoc DirectiveLoc) {
2364 int64_t Encoding = 0;
2365 if (getParser().ParseAbsoluteExpression(Encoding))
2366 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002367 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002368 return false;
2369
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002370 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002371 return TokError("unsupported encoding.");
2372
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002373 if (getLexer().isNot(AsmToken::Comma))
2374 return TokError("unexpected token in directive");
2375 Lex();
2376
2377 StringRef Name;
2378 if (getParser().ParseIdentifier(Name))
2379 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002380
2381 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2382
2383 if (IDVal == ".cfi_personality")
Rafael Espindola3a83c402010-12-27 00:36:05 +00002384 return getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002385 else {
2386 assert(IDVal == ".cfi_lsda");
Rafael Espindolabdc31672010-12-27 15:56:22 +00002387 return getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002388 }
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002389}
2390
Rafael Espindolafe024d02010-12-28 18:36:23 +00002391/// ParseDirectiveCFIRememberState
2392/// ::= .cfi_remember_state
2393bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2394 SMLoc DirectiveLoc) {
2395 return getStreamer().EmitCFIRememberState();
2396}
2397
2398/// ParseDirectiveCFIRestoreState
2399/// ::= .cfi_remember_state
2400bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2401 SMLoc DirectiveLoc) {
2402 return getStreamer().EmitCFIRestoreState();
2403}
2404
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002405/// ParseDirectiveMacrosOnOff
2406/// ::= .macros_on
2407/// ::= .macros_off
2408bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2409 SMLoc DirectiveLoc) {
2410 if (getLexer().isNot(AsmToken::EndOfStatement))
2411 return Error(getLexer().getLoc(),
2412 "unexpected token in '" + Directive + "' directive");
2413
2414 getParser().MacrosEnabled = Directive == ".macros_on";
2415
2416 return false;
2417}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002418
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002419/// ParseDirectiveMacro
2420/// ::= .macro name
2421bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2422 SMLoc DirectiveLoc) {
2423 StringRef Name;
2424 if (getParser().ParseIdentifier(Name))
2425 return TokError("expected identifier in directive");
2426
2427 if (getLexer().isNot(AsmToken::EndOfStatement))
2428 return TokError("unexpected token in '.macro' directive");
2429
2430 // Eat the end of statement.
2431 Lex();
2432
2433 AsmToken EndToken, StartToken = getTok();
2434
2435 // Lex the macro definition.
2436 for (;;) {
2437 // Check whether we have reached the end of the file.
2438 if (getLexer().is(AsmToken::Eof))
2439 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2440
2441 // Otherwise, check whether we have reach the .endmacro.
2442 if (getLexer().is(AsmToken::Identifier) &&
2443 (getTok().getIdentifier() == ".endm" ||
2444 getTok().getIdentifier() == ".endmacro")) {
2445 EndToken = getTok();
2446 Lex();
2447 if (getLexer().isNot(AsmToken::EndOfStatement))
2448 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2449 "' directive");
2450 break;
2451 }
2452
2453 // Otherwise, scan til the end of the statement.
2454 getParser().EatToEndOfStatement();
2455 }
2456
2457 if (getParser().MacroMap.lookup(Name)) {
2458 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2459 }
2460
2461 const char *BodyStart = StartToken.getLoc().getPointer();
2462 const char *BodyEnd = EndToken.getLoc().getPointer();
2463 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2464 getParser().MacroMap[Name] = new Macro(Name, Body);
2465 return false;
2466}
2467
2468/// ParseDirectiveEndMacro
2469/// ::= .endm
2470/// ::= .endmacro
2471bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2472 SMLoc DirectiveLoc) {
2473 if (getLexer().isNot(AsmToken::EndOfStatement))
2474 return TokError("unexpected token in '" + Directive + "' directive");
2475
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002476 // If we are inside a macro instantiation, terminate the current
2477 // instantiation.
2478 if (!getParser().ActiveMacros.empty()) {
2479 getParser().HandleMacroExit();
2480 return false;
2481 }
2482
2483 // Otherwise, this .endmacro is a stray entry in the file; well formed
2484 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002485 return TokError("unexpected '" + Directive + "' in file, "
2486 "no current macro definition");
2487}
2488
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002489bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002490 getParser().CheckForValidSection();
2491
2492 const MCExpr *Value;
2493
2494 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002495 return true;
2496
2497 if (getLexer().isNot(AsmToken::EndOfStatement))
2498 return TokError("unexpected token in directive");
2499
2500 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002501 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002502 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002503 getStreamer().EmitULEB128Value(Value);
2504
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002505 return false;
2506}
2507
2508
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002509/// \brief Create an MCAsmParser instance.
2510MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2511 MCContext &C, MCStreamer &Out,
2512 const MCAsmInfo &MAI) {
2513 return new AsmParser(T, SM, C, Out, MAI);
2514}