blob: 7ffe2a467806eddb2139ec804afecc701824f621 [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"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000022#include "llvm/MC/MCInst.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000023#include "llvm/MC/MCParser/AsmCond.h"
24#include "llvm/MC/MCParser/AsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
27#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000028#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000029#include "llvm/MC/MCSymbol.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000030#include "llvm/MC/MCDwarf.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000031#include "llvm/Support/Compiler.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000032#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000033#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000034#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000035#include "llvm/Target/TargetAsmParser.h"
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
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000171 bool ParseAssignment(StringRef Name);
172
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);
176
177 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
178 /// and set \arg Res to the identifier contents.
179 bool ParseIdentifier(StringRef &Res);
180
181 // Directive Parsing.
182 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
183 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000184 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000185 bool ParseDirectiveFill(); // ".fill"
186 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000187 bool ParseDirectiveZero(); // ".zero"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000188 bool ParseDirectiveSet(); // ".set"
189 bool ParseDirectiveOrg(); // ".org"
190 // ".align{,32}", ".p2align{,w,l}"
191 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
192
193 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
194 /// accepts a single symbol (which should be a label or an external).
195 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
196 bool ParseDirectiveELFType(); // ELF specific ".type"
197
198 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
199
200 bool ParseDirectiveAbort(); // ".abort"
201 bool ParseDirectiveInclude(); // ".include"
202
203 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
204 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
205 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
206 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
207
208 /// ParseEscapedString - Parse the current token as a string which may include
209 /// escaped characters and return the string contents.
210 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000211
212 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
213 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000214};
215
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000216/// \brief Generic implementations of directive handling, etc. which is shared
217/// (or the default, at least) for all assembler parser.
218class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000219 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
220 void AddDirectiveHandler(StringRef Directive) {
221 getParser().AddDirectiveHandler(this, Directive,
222 HandleDirective<GenericAsmParser, Handler>);
223 }
224
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000225public:
226 GenericAsmParser() {}
227
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000228 AsmParser &getParser() {
229 return (AsmParser&) this->MCAsmParserExtension::getParser();
230 }
231
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000232 virtual void Initialize(MCAsmParser &Parser) {
233 // Call the base implementation.
234 this->MCAsmParserExtension::Initialize(Parser);
235
236 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000237 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000240
241 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000242 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
243 ".macros_on");
244 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
245 ".macros_off");
246 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
248 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000249
250 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
251 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000252 }
253
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000254 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
255 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
256 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000257
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000258 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000259 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
260 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000261
262 void ParseUleb128(uint64_t Value);
263 void ParseSleb128(int64_t Value);
264 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000265};
266
267}
268
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000269namespace llvm {
270
271extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000272extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000273
274}
275
Chris Lattneraaec2052010-01-19 19:46:13 +0000276enum { DEFAULT_ADDRSPACE = 0 };
277
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000278AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
279 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000280 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000281 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000282 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000283 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000284
285 // Initialize the generic parser.
286 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000287
288 // Initialize the platform / file format parser.
289 //
290 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
291 // created.
292 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000293 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000294 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000295 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000296 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000297 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000298 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000299}
300
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000301AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000302 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
303
304 // Destroy any macros.
305 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
306 ie = MacroMap.end(); it != ie; ++it)
307 delete it->getValue();
308
Daniel Dunbare4749702010-07-12 18:12:02 +0000309 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000310 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000311}
312
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000313void AsmParser::PrintMacroInstantiations() {
314 // Print the active macro instantiation stack.
315 for (std::vector<MacroInstantiation*>::const_reverse_iterator
316 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
317 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
318 "note");
319}
320
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000321void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000322 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000323 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000324}
325
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000326bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000327 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000328 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000329 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000330 return true;
331}
332
Sean Callananfd0b0282010-01-21 00:19:58 +0000333bool AsmParser::EnterIncludeFile(const std::string &Filename) {
334 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
335 if (NewBuf == -1)
336 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000337
Sean Callananfd0b0282010-01-21 00:19:58 +0000338 CurBuffer = NewBuf;
339
340 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
341
342 return false;
343}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000344
345void AsmParser::JumpToLoc(SMLoc Loc) {
346 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
347 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
348}
349
Sean Callananfd0b0282010-01-21 00:19:58 +0000350const AsmToken &AsmParser::Lex() {
351 const AsmToken *tok = &Lexer.Lex();
352
353 if (tok->is(AsmToken::Eof)) {
354 // If this is the end of an included file, pop the parent file off the
355 // include stack.
356 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
357 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000358 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000359 tok = &Lexer.Lex();
360 }
361 }
362
363 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000364 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000365
Sean Callananfd0b0282010-01-21 00:19:58 +0000366 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000367}
368
Chris Lattner79180e22010-04-05 23:15:42 +0000369bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000370 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000371 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000372 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000373
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000374 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000375 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000376
377 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000378 AsmCond StartingCondState = TheCondState;
379
Chris Lattnerb717fb02009-07-02 21:53:43 +0000380 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000381 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000382 if (!ParseStatement()) continue;
383
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000384 // We had an error, validate that one was emitted and recover by skipping to
385 // the next line.
386 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000387 EatToEndOfStatement();
388 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000389
390 if (TheCondState.TheCond != StartingCondState.TheCond ||
391 TheCondState.Ignore != StartingCondState.Ignore)
392 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000393
394 // Check to see there are no empty DwarfFile slots.
395 const std::vector<MCDwarfFile *> &MCDwarfFiles =
396 getContext().getMCDwarfFiles();
397 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000398 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000399 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000400 }
Chris Lattnerb717fb02009-07-02 21:53:43 +0000401
Chris Lattner79180e22010-04-05 23:15:42 +0000402 // Finalize the output stream if there are no errors and if the client wants
403 // us to.
404 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000405 Out.Finish();
406
Chris Lattnerb717fb02009-07-02 21:53:43 +0000407 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000408}
409
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000410void AsmParser::CheckForValidSection() {
411 if (!getStreamer().getCurrentSection()) {
412 TokError("expected section directive before assembly directive");
413 Out.SwitchSection(Ctx.getMachOSection(
414 "__TEXT", "__text",
415 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
416 0, SectionKind::getText()));
417 }
418}
419
Chris Lattner2cf5f142009-06-22 01:29:09 +0000420/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
421void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000422 while (Lexer.isNot(AsmToken::EndOfStatement) &&
423 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000424 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000425
426 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000427 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000428 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000429}
430
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000431StringRef AsmParser::ParseStringToEndOfStatement() {
432 const char *Start = getTok().getLoc().getPointer();
433
434 while (Lexer.isNot(AsmToken::EndOfStatement) &&
435 Lexer.isNot(AsmToken::Eof))
436 Lex();
437
438 const char *End = getTok().getLoc().getPointer();
439 return StringRef(Start, End - Start);
440}
Chris Lattnerc4193832009-06-22 05:51:26 +0000441
Chris Lattner74ec1a32009-06-22 06:32:03 +0000442/// ParseParenExpr - Parse a paren expression and return it.
443/// NOTE: This assumes the leading '(' has already been consumed.
444///
445/// parenexpr ::= expr)
446///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000447bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000448 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000449 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000450 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000451 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000452 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000453 return false;
454}
Chris Lattnerc4193832009-06-22 05:51:26 +0000455
Chris Lattner74ec1a32009-06-22 06:32:03 +0000456/// ParsePrimaryExpr - Parse a primary expression and return it.
457/// primaryexpr ::= (parenexpr
458/// primaryexpr ::= symbol
459/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000460/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000461/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000462bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000463 switch (Lexer.getKind()) {
464 default:
465 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000466 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000467 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000468 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000469 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000470 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000471 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000472 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000473 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000474 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000475 EndLoc = Lexer.getLoc();
476
477 StringRef Identifier;
478 if (ParseIdentifier(Identifier))
479 return false;
480
Daniel Dunbarfffff912009-10-16 01:34:54 +0000481 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000482 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000483 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000484
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000485 // Mark the symbol as used in an expression.
486 Sym->setUsedInExpr(true);
487
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000488 // Lookup the symbol variant if used.
489 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000490 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000491 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000492 if (Variant == MCSymbolRefExpr::VK_Invalid) {
493 Variant = MCSymbolRefExpr::VK_None;
494 TokError("invalid variant '" + Split.second + "'");
495 }
496 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000497
Daniel Dunbarfffff912009-10-16 01:34:54 +0000498 // If this is an absolute variable reference, substitute it now to preserve
499 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000500 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000501 if (Variant)
502 return Error(EndLoc, "unexpected modified on variable reference");
503
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000504 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000505 return false;
506 }
507
508 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000509 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000510 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000511 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000512 case AsmToken::Integer: {
513 SMLoc Loc = getTok().getLoc();
514 int64_t IntVal = getTok().getIntVal();
515 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000516 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000517 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000518 // Look for 'b' or 'f' following an Integer as a directional label
519 if (Lexer.getKind() == AsmToken::Identifier) {
520 StringRef IDVal = getTok().getString();
521 if (IDVal == "f" || IDVal == "b"){
522 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
523 IDVal == "f" ? 1 : 0);
524 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
525 getContext());
526 if(IDVal == "b" && Sym->isUndefined())
527 return Error(Loc, "invalid reference to undefined symbol");
528 EndLoc = Lexer.getLoc();
529 Lex(); // Eat identifier.
530 }
531 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000532 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000533 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000534 case AsmToken::Dot: {
535 // This is a '.' reference, which references the current PC. Emit a
536 // temporary label to the streamer and refer to it.
537 MCSymbol *Sym = Ctx.CreateTempSymbol();
538 Out.EmitLabel(Sym);
539 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
540 EndLoc = Lexer.getLoc();
541 Lex(); // Eat identifier.
542 return false;
543 }
544
Daniel Dunbar3f872332009-07-28 16:08:33 +0000545 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000546 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000547 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000548 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000549 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000550 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000551 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000552 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000553 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000554 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000555 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000556 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000557 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000558 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000559 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000560 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000561 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000562 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000563 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000564 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000565 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000566 }
567}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000568
Chris Lattnerb4307b32010-01-15 19:28:38 +0000569bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000570 SMLoc EndLoc;
571 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000572}
573
Daniel Dunbarcceba832010-09-17 02:47:07 +0000574const MCExpr *
575AsmParser::ApplyModifierToExpr(const MCExpr *E,
576 MCSymbolRefExpr::VariantKind Variant) {
577 // Recurse over the given expression, rebuilding it to apply the given variant
578 // if there is exactly one symbol.
579 switch (E->getKind()) {
580 case MCExpr::Target:
581 case MCExpr::Constant:
582 return 0;
583
584 case MCExpr::SymbolRef: {
585 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
586
587 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
588 TokError("invalid variant on expression '" +
589 getTok().getIdentifier() + "' (already modified)");
590 return E;
591 }
592
593 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
594 }
595
596 case MCExpr::Unary: {
597 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
598 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
599 if (!Sub)
600 return 0;
601 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
602 }
603
604 case MCExpr::Binary: {
605 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
606 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
607 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
608
609 if (!LHS && !RHS)
610 return 0;
611
612 if (!LHS) LHS = BE->getLHS();
613 if (!RHS) RHS = BE->getRHS();
614
615 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
616 }
617 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000618
619 assert(0 && "Invalid expression kind!");
620 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000621}
622
Chris Lattner74ec1a32009-06-22 06:32:03 +0000623/// ParseExpression - Parse an expression and return it.
624///
625/// expr ::= expr +,- expr -> lowest.
626/// expr ::= expr |,^,&,! expr -> middle.
627/// expr ::= expr *,/,%,<<,>> expr -> highest.
628/// expr ::= primaryexpr
629///
Chris Lattner54482b42010-01-15 19:39:23 +0000630bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000631 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000632 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000633 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
634 return true;
635
Daniel Dunbarcceba832010-09-17 02:47:07 +0000636 // As a special case, we support 'a op b @ modifier' by rewriting the
637 // expression to include the modifier. This is inefficient, but in general we
638 // expect users to use 'a@modifier op b'.
639 if (Lexer.getKind() == AsmToken::At) {
640 Lex();
641
642 if (Lexer.isNot(AsmToken::Identifier))
643 return TokError("unexpected symbol modifier following '@'");
644
645 MCSymbolRefExpr::VariantKind Variant =
646 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
647 if (Variant == MCSymbolRefExpr::VK_Invalid)
648 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
649
650 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
651 if (!ModifiedRes) {
652 return TokError("invalid modifier '" + getTok().getIdentifier() +
653 "' (no symbols present)");
654 return true;
655 }
656
657 Res = ModifiedRes;
658 Lex();
659 }
660
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000661 // Try to constant fold it up front, if possible.
662 int64_t Value;
663 if (Res->EvaluateAsAbsolute(Value))
664 Res = MCConstantExpr::Create(Value, getContext());
665
666 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000667}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000668
Chris Lattnerb4307b32010-01-15 19:28:38 +0000669bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000670 Res = 0;
671 return ParseParenExpr(Res, EndLoc) ||
672 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000673}
674
Daniel Dunbar475839e2009-06-29 20:37:27 +0000675bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000676 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000677
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000678 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000679 if (ParseExpression(Expr))
680 return true;
681
Daniel Dunbare00b0112009-10-16 01:57:52 +0000682 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000683 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000684
685 return false;
686}
687
Daniel Dunbar3f872332009-07-28 16:08:33 +0000688static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000689 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000690 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000691 default:
692 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000693
Daniel Dunbarcceba832010-09-17 02:47:07 +0000694 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000695 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000696 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000697 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000698 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000699 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000700 return 1;
701
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000702
703 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000704 //
705 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000706 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000707 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000708 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000709 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000710 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000711 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000712 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000713 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000714 return 2;
715
716 // Intermediate Precedence: +, -, ==, !=, <>, <, <=, >, >=
717 case AsmToken::Plus:
718 Kind = MCBinaryExpr::Add;
719 return 3;
720 case AsmToken::Minus:
721 Kind = MCBinaryExpr::Sub;
722 return 3;
723 case AsmToken::EqualEqual:
724 Kind = MCBinaryExpr::EQ;
725 return 3;
726 case AsmToken::ExclaimEqual:
727 case AsmToken::LessGreater:
728 Kind = MCBinaryExpr::NE;
729 return 3;
730 case AsmToken::Less:
731 Kind = MCBinaryExpr::LT;
732 return 3;
733 case AsmToken::LessEqual:
734 Kind = MCBinaryExpr::LTE;
735 return 3;
736 case AsmToken::Greater:
737 Kind = MCBinaryExpr::GT;
738 return 3;
739 case AsmToken::GreaterEqual:
740 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000741 return 3;
742
743 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000744 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000745 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000746 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000747 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000748 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000749 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000750 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000751 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000752 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000753 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000754 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000755 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000756 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000757 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000758 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000759 }
760}
761
762
763/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
764/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000765bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
766 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000767 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000768 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000769 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000770
771 // If the next token is lower precedence than we are allowed to eat, return
772 // successfully with what we ate already.
773 if (TokPrec < Precedence)
774 return false;
775
Sean Callanan79ed1a82010-01-19 20:22:31 +0000776 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000777
778 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000779 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000780 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000781
782 // If BinOp binds less tightly with RHS than the operator after RHS, let
783 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000784 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000785 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000786 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000787 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000788 }
789
Daniel Dunbar475839e2009-06-29 20:37:27 +0000790 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000791 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000792 }
793}
794
Chris Lattnerc4193832009-06-22 05:51:26 +0000795
796
797
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000798/// ParseStatement:
799/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000800/// ::= Label* Directive ...Operands... EndOfStatement
801/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000802bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000803 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000804 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000805 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000806 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000807 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000808
809 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000810 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000811 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000812 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000813 int64_t LocalLabelVal = -1;
814 // GUESS allow an integer followed by a ':' as a directional local label
815 if (Lexer.is(AsmToken::Integer)) {
816 LocalLabelVal = getTok().getIntVal();
817 if (LocalLabelVal < 0) {
818 if (!TheCondState.Ignore)
819 return TokError("unexpected token at start of statement");
820 IDVal = "";
821 }
822 else {
823 IDVal = getTok().getString();
824 Lex(); // Consume the integer token to be used as an identifier token.
825 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000826 if (!TheCondState.Ignore)
827 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000828 }
829 }
830 }
831 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000832 if (!TheCondState.Ignore)
833 return TokError("unexpected token at start of statement");
834 IDVal = "";
835 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000836
Chris Lattner7834fac2010-04-17 18:14:27 +0000837 // Handle conditional assembly here before checking for skipping. We
838 // have to do this so that .endif isn't skipped in a ".if 0" block for
839 // example.
840 if (IDVal == ".if")
841 return ParseDirectiveIf(IDLoc);
842 if (IDVal == ".elseif")
843 return ParseDirectiveElseIf(IDLoc);
844 if (IDVal == ".else")
845 return ParseDirectiveElse(IDLoc);
846 if (IDVal == ".endif")
847 return ParseDirectiveEndIf(IDLoc);
848
849 // If we are in a ".if 0" block, ignore this statement.
850 if (TheCondState.Ignore) {
851 EatToEndOfStatement();
852 return false;
853 }
854
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000855 // FIXME: Recurse on local labels?
856
857 // See what kind of statement we have.
858 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000859 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000860 CheckForValidSection();
861
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000862 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000863 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000864
865 // Diagnose attempt to use a variable as a label.
866 //
867 // FIXME: Diagnostics. Note the location of the definition as a label.
868 // FIXME: This doesn't diagnose assignment to a symbol which has been
869 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000870 MCSymbol *Sym;
871 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000872 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000873 else
874 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000875 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000876 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000877
Daniel Dunbar959fd882009-08-26 22:13:22 +0000878 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000879 Out.EmitLabel(Sym);
880
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000881 // Consume any end of statement token, if present, to avoid spurious
882 // AddBlankLine calls().
883 if (Lexer.is(AsmToken::EndOfStatement)) {
884 Lex();
885 if (Lexer.is(AsmToken::Eof))
886 return false;
887 }
888
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000889 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000890 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000891
Daniel Dunbar3f872332009-07-28 16:08:33 +0000892 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000893 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000894 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000895
Daniel Dunbare2ace502009-08-31 08:09:09 +0000896 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000897
898 default: // Normal instruction or directive.
899 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000900 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000901
902 // If macros are enabled, check to see if this is a macro instantiation.
903 if (MacrosEnabled)
904 if (const Macro *M = MacroMap.lookup(IDVal))
905 return HandleMacroEntry(IDVal, IDLoc, M);
906
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000907 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000908 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000909 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000910 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000911 return ParseDirectiveSet();
912
Daniel Dunbara0d14262009-06-24 23:30:00 +0000913 // Data directives
914
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000915 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000916 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000917 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000918 return ParseDirectiveAscii(true);
919
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000920 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000921 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000922 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000923 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000924 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000925 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000926 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000927 return ParseDirectiveValue(8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000928 if (IDVal == ".single")
929 return ParseDirectiveRealValue(APFloat::IEEEsingle);
930 if (IDVal == ".double")
931 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000932
Eli Friedman5d68ec22010-07-19 04:17:25 +0000933 if (IDVal == ".align") {
934 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
935 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
936 }
937 if (IDVal == ".align32") {
938 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
939 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
940 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000941 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000942 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000943 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000944 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000945 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000946 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000947 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000948 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000949 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000950 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000951 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000952 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
953
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000954 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000955 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000956
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000957 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000958 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000959 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000960 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000961 if (IDVal == ".zero")
962 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000963
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000964 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000965
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000966 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000967 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000968 // ELF only? Should it be here?
969 if (IDVal == ".local")
970 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000971 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000972 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000973 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000974 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000975 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000976 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000977 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000978 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000979 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000980 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000981 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000982 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000983 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000984 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000985 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000986 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000987 if (IDVal == ".type")
988 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000989 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000990 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000991 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000992 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000993 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000994 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000995 if (IDVal == ".weak_def_can_be_hidden")
996 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000997
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000998 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000999 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001000 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001001 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001002
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001003 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001004 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001005 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001006 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001007
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001008 // Look up the handler in the handler table.
1009 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1010 DirectiveMap.lookup(IDVal);
1011 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001012 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001013
Kevin Enderby9c656452009-09-10 20:51:44 +00001014 // Target hook for parsing target specific directives.
1015 if (!getTargetParser().ParseDirective(ID))
1016 return false;
1017
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001018 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001019 EatToEndOfStatement();
1020 return false;
1021 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001022
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001023 CheckForValidSection();
1024
Chris Lattnera7f13542010-05-19 23:34:33 +00001025 // Canonicalize the opcode to lower case.
1026 SmallString<128> Opcode;
1027 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1028 Opcode.push_back(tolower(IDVal[i]));
1029
Chris Lattner98986712010-01-14 22:21:20 +00001030 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001031 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001032 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001033
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001034 // Dump the parsed representation, if requested.
1035 if (getShowParsedOperands()) {
1036 SmallString<256> Str;
1037 raw_svector_ostream OS(Str);
1038 OS << "parsed instruction: [";
1039 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1040 if (i != 0)
1041 OS << ", ";
1042 ParsedOperands[i]->dump(OS);
1043 }
1044 OS << "]";
1045
1046 PrintMessage(IDLoc, OS.str(), "note");
1047 }
1048
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001049 // If parsing succeeded, match the instruction.
1050 if (!HadError) {
1051 MCInst Inst;
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001052 if (!getTargetParser().MatchInstruction(IDLoc, ParsedOperands, Inst)) {
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001053 // Emit the instruction on success.
1054 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001055 } else
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001056 HadError = true;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001057 }
Chris Lattner98986712010-01-14 22:21:20 +00001058
Chris Lattner98986712010-01-14 22:21:20 +00001059 // Free any parsed operands.
1060 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1061 delete ParsedOperands[i];
1062
Chris Lattnercbf8a982010-09-11 16:18:25 +00001063 // Don't skip the rest of the line, the instruction parser is responsible for
1064 // that.
1065 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001066}
Chris Lattner9a023f72009-06-24 04:43:34 +00001067
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001068MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1069 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001070 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1071{
1072 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1073 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001074 SmallString<256> Buf;
1075 raw_svector_ostream OS(Buf);
1076
1077 StringRef Body = M->Body;
1078 while (!Body.empty()) {
1079 // Scan for the next substitution.
1080 std::size_t End = Body.size(), Pos = 0;
1081 for (; Pos != End; ++Pos) {
1082 // Check for a substitution or escape.
1083 if (Body[Pos] != '$' || Pos + 1 == End)
1084 continue;
1085
1086 char Next = Body[Pos + 1];
1087 if (Next == '$' || Next == 'n' || isdigit(Next))
1088 break;
1089 }
1090
1091 // Add the prefix.
1092 OS << Body.slice(0, Pos);
1093
1094 // Check if we reached the end.
1095 if (Pos == End)
1096 break;
1097
1098 switch (Body[Pos+1]) {
1099 // $$ => $
1100 case '$':
1101 OS << '$';
1102 break;
1103
1104 // $n => number of arguments
1105 case 'n':
1106 OS << A.size();
1107 break;
1108
1109 // $[0-9] => argument
1110 default: {
1111 // Missing arguments are ignored.
1112 unsigned Index = Body[Pos+1] - '0';
1113 if (Index >= A.size())
1114 break;
1115
1116 // Otherwise substitute with the token values, with spaces eliminated.
1117 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1118 ie = A[Index].end(); it != ie; ++it)
1119 OS << it->getString();
1120 break;
1121 }
1122 }
1123
1124 // Update the scan point.
1125 Body = Body.substr(Pos + 2);
1126 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001127
1128 // We include the .endmacro in the buffer as our queue to exit the macro
1129 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001130 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001131
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001132 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001133}
1134
1135bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1136 const Macro *M) {
1137 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1138 // this, although we should protect against infinite loops.
1139 if (ActiveMacros.size() == 20)
1140 return TokError("macros cannot be nested more than 20 levels deep");
1141
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001142 // Parse the macro instantiation arguments.
1143 std::vector<std::vector<AsmToken> > MacroArguments;
1144 MacroArguments.push_back(std::vector<AsmToken>());
1145 unsigned ParenLevel = 0;
1146 for (;;) {
1147 if (Lexer.is(AsmToken::Eof))
1148 return TokError("unexpected token in macro instantiation");
1149 if (Lexer.is(AsmToken::EndOfStatement))
1150 break;
1151
1152 // If we aren't inside parentheses and this is a comma, start a new token
1153 // list.
1154 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1155 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001156 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001157 // Adjust the current parentheses level.
1158 if (Lexer.is(AsmToken::LParen))
1159 ++ParenLevel;
1160 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1161 --ParenLevel;
1162
1163 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001164 MacroArguments.back().push_back(getTok());
1165 }
1166 Lex();
1167 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001168
1169 // Create the macro instantiation object and add to the current macro
1170 // instantiation stack.
1171 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001172 getTok().getLoc(),
1173 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001174 ActiveMacros.push_back(MI);
1175
1176 // Jump to the macro instantiation and prime the lexer.
1177 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1178 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1179 Lex();
1180
1181 return false;
1182}
1183
1184void AsmParser::HandleMacroExit() {
1185 // Jump to the EndOfStatement we should return to, and consume it.
1186 JumpToLoc(ActiveMacros.back()->ExitLoc);
1187 Lex();
1188
1189 // Pop the instantiation entry.
1190 delete ActiveMacros.back();
1191 ActiveMacros.pop_back();
1192}
1193
Benjamin Kramer38e59892010-07-14 22:38:02 +00001194bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001195 // FIXME: Use better location, we should use proper tokens.
1196 SMLoc EqualLoc = Lexer.getLoc();
1197
Daniel Dunbar821e3332009-08-31 08:09:28 +00001198 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001199 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001200 return true;
1201
Daniel Dunbar3f872332009-07-28 16:08:33 +00001202 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001203 return TokError("unexpected token in assignment");
1204
1205 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001206 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001207
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001208 // Validate that the LHS is allowed to be a variable (either it has not been
1209 // used as a symbol, or it is an absolute symbol).
1210 MCSymbol *Sym = getContext().LookupSymbol(Name);
1211 if (Sym) {
1212 // Diagnose assignment to a label.
1213 //
1214 // FIXME: Diagnostics. Note the location of the definition as a label.
1215 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001216 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1217 ; // Allow redefinitions of undefined symbols only used in directives.
1218 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001219 return Error(EqualLoc, "redefinition of '" + Name + "'");
1220 else if (!Sym->isVariable())
1221 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001222 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001223 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1224 Name + "'");
1225 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001226 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001227
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001228 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001229
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001230 Sym->setUsedInExpr(true);
1231
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001232 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001233 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001234
1235 return false;
1236}
1237
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001238/// ParseIdentifier:
1239/// ::= identifier
1240/// ::= string
1241bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001242 // The assembler has relaxed rules for accepting identifiers, in particular we
1243 // allow things like '.globl $foo', which would normally be separate
1244 // tokens. At this level, we have already lexed so we cannot (currently)
1245 // handle this as a context dependent token, instead we detect adjacent tokens
1246 // and return the combined identifier.
1247 if (Lexer.is(AsmToken::Dollar)) {
1248 SMLoc DollarLoc = getLexer().getLoc();
1249
1250 // Consume the dollar sign, and check for a following identifier.
1251 Lex();
1252 if (Lexer.isNot(AsmToken::Identifier))
1253 return true;
1254
1255 // We have a '$' followed by an identifier, make sure they are adjacent.
1256 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1257 return true;
1258
1259 // Construct the joined identifier and consume the token.
1260 Res = StringRef(DollarLoc.getPointer(),
1261 getTok().getIdentifier().size() + 1);
1262 Lex();
1263 return false;
1264 }
1265
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001266 if (Lexer.isNot(AsmToken::Identifier) &&
1267 Lexer.isNot(AsmToken::String))
1268 return true;
1269
Sean Callanan18b83232010-01-19 21:44:56 +00001270 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001271
Sean Callanan79ed1a82010-01-19 20:22:31 +00001272 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001273
1274 return false;
1275}
1276
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001277/// ParseDirectiveSet:
1278/// ::= .set identifier ',' expression
1279bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001280 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001281
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001282 if (ParseIdentifier(Name))
1283 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001284
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001285 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001286 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001287 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001288
Daniel Dunbare2ace502009-08-31 08:09:09 +00001289 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001290}
1291
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001292bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001293 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001294
1295 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001296 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001297 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1298 if (Str[i] != '\\') {
1299 Data += Str[i];
1300 continue;
1301 }
1302
1303 // Recognize escaped characters. Note that this escape semantics currently
1304 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1305 ++i;
1306 if (i == e)
1307 return TokError("unexpected backslash at end of string");
1308
1309 // Recognize octal sequences.
1310 if ((unsigned) (Str[i] - '0') <= 7) {
1311 // Consume up to three octal characters.
1312 unsigned Value = Str[i] - '0';
1313
1314 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1315 ++i;
1316 Value = Value * 8 + (Str[i] - '0');
1317
1318 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1319 ++i;
1320 Value = Value * 8 + (Str[i] - '0');
1321 }
1322 }
1323
1324 if (Value > 255)
1325 return TokError("invalid octal escape sequence (out of range)");
1326
1327 Data += (unsigned char) Value;
1328 continue;
1329 }
1330
1331 // Otherwise recognize individual escapes.
1332 switch (Str[i]) {
1333 default:
1334 // Just reject invalid escape sequences for now.
1335 return TokError("invalid escape sequence (unrecognized character)");
1336
1337 case 'b': Data += '\b'; break;
1338 case 'f': Data += '\f'; break;
1339 case 'n': Data += '\n'; break;
1340 case 'r': Data += '\r'; break;
1341 case 't': Data += '\t'; break;
1342 case '"': Data += '"'; break;
1343 case '\\': Data += '\\'; break;
1344 }
1345 }
1346
1347 return false;
1348}
1349
Daniel Dunbara0d14262009-06-24 23:30:00 +00001350/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001351/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001352bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001353 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001354 CheckForValidSection();
1355
Daniel Dunbara0d14262009-06-24 23:30:00 +00001356 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001357 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001358 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001359
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001360 std::string Data;
1361 if (ParseEscapedString(Data))
1362 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001363
1364 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001365 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001366 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1367
Sean Callanan79ed1a82010-01-19 20:22:31 +00001368 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001369
1370 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001371 break;
1372
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001373 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001374 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001375 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001376 }
1377 }
1378
Sean Callanan79ed1a82010-01-19 20:22:31 +00001379 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001380 return false;
1381}
1382
1383/// ParseDirectiveValue
1384/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1385bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001386 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001387 CheckForValidSection();
1388
Daniel Dunbara0d14262009-06-24 23:30:00 +00001389 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001390 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001391 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001392 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001393 return true;
1394
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001395 // Special case constant expressions to match code generator.
1396 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001397 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001398 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001399 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001400
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001401 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001402 break;
1403
1404 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001405 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001406 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001407 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001408 }
1409 }
1410
Sean Callanan79ed1a82010-01-19 20:22:31 +00001411 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001412 return false;
1413}
1414
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001415/// ParseDirectiveRealValue
1416/// ::= (.single | .double) [ expression (, expression)* ]
1417bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1418 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1419 CheckForValidSection();
1420
1421 for (;;) {
1422 // We don't truly support arithmetic on floating point expressions, so we
1423 // have to manually parse unary prefixes.
1424 bool IsNeg = false;
1425 if (getLexer().is(AsmToken::Minus)) {
1426 Lex();
1427 IsNeg = true;
1428 } else if (getLexer().is(AsmToken::Plus))
1429 Lex();
1430
1431 if (getLexer().isNot(AsmToken::Integer) &&
1432 getLexer().isNot(AsmToken::Real))
1433 return TokError("unexpected token in directive");
1434
1435 // Convert to an APFloat.
1436 APFloat Value(Semantics);
1437 if (Value.convertFromString(getTok().getString(),
1438 APFloat::rmNearestTiesToEven) ==
1439 APFloat::opInvalidOp)
1440 return TokError("invalid floating point literal");
1441 if (IsNeg)
1442 Value.changeSign();
1443
1444 // Consume the numeric token.
1445 Lex();
1446
1447 // Emit the value as an integer.
1448 APInt AsInt = Value.bitcastToAPInt();
1449 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1450 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1451
1452 if (getLexer().is(AsmToken::EndOfStatement))
1453 break;
1454
1455 if (getLexer().isNot(AsmToken::Comma))
1456 return TokError("unexpected token in directive");
1457 Lex();
1458 }
1459 }
1460
1461 Lex();
1462 return false;
1463}
1464
Daniel Dunbara0d14262009-06-24 23:30:00 +00001465/// ParseDirectiveSpace
1466/// ::= .space expression [ , expression ]
1467bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001468 CheckForValidSection();
1469
Daniel Dunbara0d14262009-06-24 23:30:00 +00001470 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001471 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001472 return true;
1473
1474 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001475 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1476 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001477 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001478 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001479
Daniel Dunbar475839e2009-06-29 20:37:27 +00001480 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001481 return true;
1482
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001483 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001484 return TokError("unexpected token in '.space' directive");
1485 }
1486
Sean Callanan79ed1a82010-01-19 20:22:31 +00001487 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001488
1489 if (NumBytes <= 0)
1490 return TokError("invalid number of bytes in '.space' directive");
1491
1492 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001493 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001494
1495 return false;
1496}
1497
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001498/// ParseDirectiveZero
1499/// ::= .zero expression
1500bool AsmParser::ParseDirectiveZero() {
1501 CheckForValidSection();
1502
1503 int64_t NumBytes;
1504 if (ParseAbsoluteExpression(NumBytes))
1505 return true;
1506
1507 if (getLexer().isNot(AsmToken::EndOfStatement))
1508 return TokError("unexpected token in '.zero' directive");
1509
1510 Lex();
1511
1512 getStreamer().EmitFill(NumBytes, 0, DEFAULT_ADDRSPACE);
1513
1514 return false;
1515}
1516
Daniel Dunbara0d14262009-06-24 23:30:00 +00001517/// ParseDirectiveFill
1518/// ::= .fill expression , expression , expression
1519bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001520 CheckForValidSection();
1521
Daniel Dunbara0d14262009-06-24 23:30:00 +00001522 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001523 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001524 return true;
1525
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001526 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001527 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001528 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001529
1530 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001531 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001532 return true;
1533
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001534 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001535 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001536 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001537
1538 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001539 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001540 return true;
1541
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001542 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001543 return TokError("unexpected token in '.fill' directive");
1544
Sean Callanan79ed1a82010-01-19 20:22:31 +00001545 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001546
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001547 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1548 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001549
1550 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001551 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001552
1553 return false;
1554}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001555
1556/// ParseDirectiveOrg
1557/// ::= .org expression [ , expression ]
1558bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001559 CheckForValidSection();
1560
Daniel Dunbar821e3332009-08-31 08:09:28 +00001561 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001562 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001563 return true;
1564
1565 // Parse optional fill expression.
1566 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001567 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1568 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001569 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001570 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001571
Daniel Dunbar475839e2009-06-29 20:37:27 +00001572 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001573 return true;
1574
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001575 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001576 return TokError("unexpected token in '.org' directive");
1577 }
1578
Sean Callanan79ed1a82010-01-19 20:22:31 +00001579 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001580
1581 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1582 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001583 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001584
1585 return false;
1586}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001587
1588/// ParseDirectiveAlign
1589/// ::= {.align, ...} expression [ , expression [ , expression ]]
1590bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001591 CheckForValidSection();
1592
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001593 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001594 int64_t Alignment;
1595 if (ParseAbsoluteExpression(Alignment))
1596 return true;
1597
1598 SMLoc MaxBytesLoc;
1599 bool HasFillExpr = false;
1600 int64_t FillExpr = 0;
1601 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001602 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1603 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001604 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001605 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001606
1607 // The fill expression can be omitted while specifying a maximum number of
1608 // alignment bytes, e.g:
1609 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001610 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001611 HasFillExpr = true;
1612 if (ParseAbsoluteExpression(FillExpr))
1613 return true;
1614 }
1615
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001616 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1617 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001618 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001619 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001620
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001621 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001622 if (ParseAbsoluteExpression(MaxBytesToFill))
1623 return true;
1624
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001625 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001626 return TokError("unexpected token in directive");
1627 }
1628 }
1629
Sean Callanan79ed1a82010-01-19 20:22:31 +00001630 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001631
Daniel Dunbar648ac512010-05-17 21:54:30 +00001632 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001633 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001634
1635 // Compute alignment in bytes.
1636 if (IsPow2) {
1637 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001638 if (Alignment >= 32) {
1639 Error(AlignmentLoc, "invalid alignment value");
1640 Alignment = 31;
1641 }
1642
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001643 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001644 }
1645
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001646 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001647 if (MaxBytesLoc.isValid()) {
1648 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001649 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1650 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001651 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001652 }
1653
1654 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001655 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1656 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001657 MaxBytesToFill = 0;
1658 }
1659 }
1660
Daniel Dunbar648ac512010-05-17 21:54:30 +00001661 // Check whether we should use optimal code alignment for this .align
1662 // directive.
1663 //
1664 // FIXME: This should be using a target hook.
1665 bool UseCodeAlign = false;
1666 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001667 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001668 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001669 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1670 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001671 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001672 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001673 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001674 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1675 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001676 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001677
1678 return false;
1679}
1680
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001681/// ParseDirectiveSymbolAttribute
1682/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001683bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001684 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001685 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001686 StringRef Name;
1687
1688 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001689 return TokError("expected identifier in directive");
1690
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001691 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001692
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001693 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001694
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001696 break;
1697
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001698 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001699 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001700 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001701 }
1702 }
1703
Sean Callanan79ed1a82010-01-19 20:22:31 +00001704 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001705 return false;
1706}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001707
Matt Fleming924c5e52010-05-21 11:36:59 +00001708/// ParseDirectiveELFType
1709/// ::= .type identifier , @attribute
1710bool AsmParser::ParseDirectiveELFType() {
1711 StringRef Name;
1712 if (ParseIdentifier(Name))
1713 return TokError("expected identifier in directive");
1714
1715 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001716 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001717
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001718 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001719 return TokError("unexpected token in '.type' directive");
1720 Lex();
1721
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001722 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001723 return TokError("expected '@' before type");
1724 Lex();
1725
1726 StringRef Type;
1727 SMLoc TypeLoc;
1728
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001729 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001730 if (ParseIdentifier(Type))
1731 return TokError("expected symbol type in directive");
1732
1733 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1734 .Case("function", MCSA_ELF_TypeFunction)
1735 .Case("object", MCSA_ELF_TypeObject)
1736 .Case("tls_object", MCSA_ELF_TypeTLS)
1737 .Case("common", MCSA_ELF_TypeCommon)
1738 .Case("notype", MCSA_ELF_TypeNoType)
1739 .Default(MCSA_Invalid);
1740
1741 if (Attr == MCSA_Invalid)
1742 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1743
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001744 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001745 return TokError("unexpected token in '.type' directive");
1746
1747 Lex();
1748
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001749 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001750
1751 return false;
1752}
1753
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001754/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001755/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1756bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001757 CheckForValidSection();
1758
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001759 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001760 StringRef Name;
1761 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001762 return TokError("expected identifier in directive");
1763
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001764 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001765 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001766
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001767 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001768 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001769 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001770
1771 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001772 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001773 if (ParseAbsoluteExpression(Size))
1774 return true;
1775
1776 int64_t Pow2Alignment = 0;
1777 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001778 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001779 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001780 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001781 if (ParseAbsoluteExpression(Pow2Alignment))
1782 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001783
1784 // If this target takes alignments in bytes (not log) validate and convert.
1785 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1786 if (!isPowerOf2_64(Pow2Alignment))
1787 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1788 Pow2Alignment = Log2_64(Pow2Alignment);
1789 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001790 }
1791
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001792 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001793 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001794
Sean Callanan79ed1a82010-01-19 20:22:31 +00001795 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001796
Chris Lattner1fc3d752009-07-09 17:25:12 +00001797 // NOTE: a size of zero for a .comm should create a undefined symbol
1798 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001799 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001800 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1801 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001802
Eric Christopherc260a3e2010-05-14 01:38:54 +00001803 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001804 // may internally end up wanting an alignment in bytes.
1805 // FIXME: Diagnose overflow.
1806 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001807 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1808 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001809
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001810 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001811 return Error(IDLoc, "invalid symbol redefinition");
1812
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001813 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001814 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001815 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001816 getStreamer().EmitZerofill(Ctx.getMachOSection(
1817 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1818 0, SectionKind::getBSS()),
1819 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001820 return false;
1821 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001822
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001823 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001824 return false;
1825}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001826
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001827/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001828/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001829bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001830 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001831 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001832
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001833 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001834 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001835 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001836
Sean Callanan79ed1a82010-01-19 20:22:31 +00001837 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001838
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001839 if (Str.empty())
1840 Error(Loc, ".abort detected. Assembly stopping.");
1841 else
1842 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001843 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001844
1845 return false;
1846}
Kevin Enderby71148242009-07-14 21:35:03 +00001847
Kevin Enderby1f049b22009-07-14 23:21:55 +00001848/// ParseDirectiveInclude
1849/// ::= .include "filename"
1850bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001851 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001852 return TokError("expected string in '.include' directive");
1853
Sean Callanan18b83232010-01-19 21:44:56 +00001854 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001855 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001856 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001857
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001858 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001859 return TokError("unexpected token in '.include' directive");
1860
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001861 // Strip the quotes.
1862 Filename = Filename.substr(1, Filename.size()-2);
1863
1864 // Attempt to switch the lexer to the included file before consuming the end
1865 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001866 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001867 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001868 return true;
1869 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001870
1871 return false;
1872}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001873
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001874/// ParseDirectiveIf
1875/// ::= .if expression
1876bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001877 TheCondStack.push_back(TheCondState);
1878 TheCondState.TheCond = AsmCond::IfCond;
1879 if(TheCondState.Ignore) {
1880 EatToEndOfStatement();
1881 }
1882 else {
1883 int64_t ExprValue;
1884 if (ParseAbsoluteExpression(ExprValue))
1885 return true;
1886
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001887 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001888 return TokError("unexpected token in '.if' directive");
1889
Sean Callanan79ed1a82010-01-19 20:22:31 +00001890 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001891
1892 TheCondState.CondMet = ExprValue;
1893 TheCondState.Ignore = !TheCondState.CondMet;
1894 }
1895
1896 return false;
1897}
1898
1899/// ParseDirectiveElseIf
1900/// ::= .elseif expression
1901bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1902 if (TheCondState.TheCond != AsmCond::IfCond &&
1903 TheCondState.TheCond != AsmCond::ElseIfCond)
1904 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1905 " an .elseif");
1906 TheCondState.TheCond = AsmCond::ElseIfCond;
1907
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001908 bool LastIgnoreState = false;
1909 if (!TheCondStack.empty())
1910 LastIgnoreState = TheCondStack.back().Ignore;
1911 if (LastIgnoreState || TheCondState.CondMet) {
1912 TheCondState.Ignore = true;
1913 EatToEndOfStatement();
1914 }
1915 else {
1916 int64_t ExprValue;
1917 if (ParseAbsoluteExpression(ExprValue))
1918 return true;
1919
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001920 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001921 return TokError("unexpected token in '.elseif' directive");
1922
Sean Callanan79ed1a82010-01-19 20:22:31 +00001923 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001924 TheCondState.CondMet = ExprValue;
1925 TheCondState.Ignore = !TheCondState.CondMet;
1926 }
1927
1928 return false;
1929}
1930
1931/// ParseDirectiveElse
1932/// ::= .else
1933bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001934 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001935 return TokError("unexpected token in '.else' directive");
1936
Sean Callanan79ed1a82010-01-19 20:22:31 +00001937 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001938
1939 if (TheCondState.TheCond != AsmCond::IfCond &&
1940 TheCondState.TheCond != AsmCond::ElseIfCond)
1941 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1942 ".elseif");
1943 TheCondState.TheCond = AsmCond::ElseCond;
1944 bool LastIgnoreState = false;
1945 if (!TheCondStack.empty())
1946 LastIgnoreState = TheCondStack.back().Ignore;
1947 if (LastIgnoreState || TheCondState.CondMet)
1948 TheCondState.Ignore = true;
1949 else
1950 TheCondState.Ignore = false;
1951
1952 return false;
1953}
1954
1955/// ParseDirectiveEndIf
1956/// ::= .endif
1957bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001958 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001959 return TokError("unexpected token in '.endif' directive");
1960
Sean Callanan79ed1a82010-01-19 20:22:31 +00001961 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001962
1963 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1964 TheCondStack.empty())
1965 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1966 ".else");
1967 if (!TheCondStack.empty()) {
1968 TheCondState = TheCondStack.back();
1969 TheCondStack.pop_back();
1970 }
1971
1972 return false;
1973}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001974
1975/// ParseDirectiveFile
1976/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001977bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001978 // FIXME: I'm not sure what this is.
1979 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001980 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001981 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001982 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001983 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001984
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001985 if (FileNumber < 1)
1986 return TokError("file number less than one");
1987 }
1988
Daniel Dunbareceec052010-07-12 17:45:27 +00001989 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001990 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001991
Chris Lattnerd32e8032010-01-25 19:02:58 +00001992 StringRef Filename = getTok().getString();
1993 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001994 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001995
Daniel Dunbareceec052010-07-12 17:45:27 +00001996 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001997 return TokError("unexpected token in '.file' directive");
1998
Chris Lattnerd32e8032010-01-25 19:02:58 +00001999 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002000 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002001 else {
2002 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
2003 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00002004 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002005 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002006
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002007 return false;
2008}
2009
2010/// ParseDirectiveLine
2011/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002012bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002013 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2014 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002015 return TokError("unexpected token in '.line' directive");
2016
Sean Callanan18b83232010-01-19 21:44:56 +00002017 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002018 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002019 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002020
2021 // FIXME: Do something with the .line.
2022 }
2023
Daniel Dunbareceec052010-07-12 17:45:27 +00002024 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002025 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002026
2027 return false;
2028}
2029
2030
2031/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002032/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002033/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2034/// The first number is a file number, must have been previously assigned with
2035/// a .file directive, the second number is the line number and optionally the
2036/// third number is a column position (zero if not specified). The remaining
2037/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002038bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002039
Daniel Dunbareceec052010-07-12 17:45:27 +00002040 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002041 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002042 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002043 if (FileNumber < 1)
2044 return TokError("file number less than one in '.loc' directive");
2045 if (!getContext().ValidateDwarfFileNumber(FileNumber))
2046 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002047 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002048
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002049 int64_t LineNumber = 0;
2050 if (getLexer().is(AsmToken::Integer)) {
2051 LineNumber = getTok().getIntVal();
2052 if (LineNumber < 1)
2053 return TokError("line number less than one in '.loc' directive");
2054 Lex();
2055 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002056
2057 int64_t ColumnPos = 0;
2058 if (getLexer().is(AsmToken::Integer)) {
2059 ColumnPos = getTok().getIntVal();
2060 if (ColumnPos < 0)
2061 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002062 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002063 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002064
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002065 unsigned Flags = 0;
2066 unsigned Isa = 0;
2067 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2068 for (;;) {
2069 if (getLexer().is(AsmToken::EndOfStatement))
2070 break;
2071
2072 StringRef Name;
2073 SMLoc Loc = getTok().getLoc();
2074 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002075 return TokError("unexpected token in '.loc' directive");
2076
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002077 if (Name == "basic_block")
2078 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2079 else if (Name == "prologue_end")
2080 Flags |= DWARF2_FLAG_PROLOGUE_END;
2081 else if (Name == "epilogue_begin")
2082 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2083 else if (Name == "is_stmt") {
2084 SMLoc Loc = getTok().getLoc();
2085 const MCExpr *Value;
2086 if (getParser().ParseExpression(Value))
2087 return true;
2088 // The expression must be the constant 0 or 1.
2089 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2090 int Value = MCE->getValue();
2091 if (Value == 0)
2092 Flags &= ~DWARF2_FLAG_IS_STMT;
2093 else if (Value == 1)
2094 Flags |= DWARF2_FLAG_IS_STMT;
2095 else
2096 return Error(Loc, "is_stmt value not 0 or 1");
2097 }
2098 else {
2099 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2100 }
2101 }
2102 else if (Name == "isa") {
2103 SMLoc Loc = getTok().getLoc();
2104 const MCExpr *Value;
2105 if (getParser().ParseExpression(Value))
2106 return true;
2107 // The expression must be a constant greater or equal to 0.
2108 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2109 int Value = MCE->getValue();
2110 if (Value < 0)
2111 return Error(Loc, "isa number less than zero");
2112 Isa = Value;
2113 }
2114 else {
2115 return Error(Loc, "isa number not a constant value");
2116 }
2117 }
2118 else {
2119 return Error(Loc, "unknown sub-directive in '.loc' directive");
2120 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002121
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002122 if (getLexer().is(AsmToken::EndOfStatement))
2123 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002124 }
2125 }
2126
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002127 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,Isa);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002128
2129 return false;
2130}
2131
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002132/// ParseDirectiveMacrosOnOff
2133/// ::= .macros_on
2134/// ::= .macros_off
2135bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2136 SMLoc DirectiveLoc) {
2137 if (getLexer().isNot(AsmToken::EndOfStatement))
2138 return Error(getLexer().getLoc(),
2139 "unexpected token in '" + Directive + "' directive");
2140
2141 getParser().MacrosEnabled = Directive == ".macros_on";
2142
2143 return false;
2144}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002145
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002146/// ParseDirectiveMacro
2147/// ::= .macro name
2148bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2149 SMLoc DirectiveLoc) {
2150 StringRef Name;
2151 if (getParser().ParseIdentifier(Name))
2152 return TokError("expected identifier in directive");
2153
2154 if (getLexer().isNot(AsmToken::EndOfStatement))
2155 return TokError("unexpected token in '.macro' directive");
2156
2157 // Eat the end of statement.
2158 Lex();
2159
2160 AsmToken EndToken, StartToken = getTok();
2161
2162 // Lex the macro definition.
2163 for (;;) {
2164 // Check whether we have reached the end of the file.
2165 if (getLexer().is(AsmToken::Eof))
2166 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2167
2168 // Otherwise, check whether we have reach the .endmacro.
2169 if (getLexer().is(AsmToken::Identifier) &&
2170 (getTok().getIdentifier() == ".endm" ||
2171 getTok().getIdentifier() == ".endmacro")) {
2172 EndToken = getTok();
2173 Lex();
2174 if (getLexer().isNot(AsmToken::EndOfStatement))
2175 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2176 "' directive");
2177 break;
2178 }
2179
2180 // Otherwise, scan til the end of the statement.
2181 getParser().EatToEndOfStatement();
2182 }
2183
2184 if (getParser().MacroMap.lookup(Name)) {
2185 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2186 }
2187
2188 const char *BodyStart = StartToken.getLoc().getPointer();
2189 const char *BodyEnd = EndToken.getLoc().getPointer();
2190 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2191 getParser().MacroMap[Name] = new Macro(Name, Body);
2192 return false;
2193}
2194
2195/// ParseDirectiveEndMacro
2196/// ::= .endm
2197/// ::= .endmacro
2198bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2199 SMLoc DirectiveLoc) {
2200 if (getLexer().isNot(AsmToken::EndOfStatement))
2201 return TokError("unexpected token in '" + Directive + "' directive");
2202
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002203 // If we are inside a macro instantiation, terminate the current
2204 // instantiation.
2205 if (!getParser().ActiveMacros.empty()) {
2206 getParser().HandleMacroExit();
2207 return false;
2208 }
2209
2210 // Otherwise, this .endmacro is a stray entry in the file; well formed
2211 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002212 return TokError("unexpected '" + Directive + "' in file, "
2213 "no current macro definition");
2214}
2215
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002216void GenericAsmParser::ParseUleb128(uint64_t Value) {
2217 const uint64_t Mask = (1 << 7) - 1;
2218 do {
2219 unsigned Byte = Value & Mask;
2220 Value >>= 7;
2221 if (Value) // Not the last one
2222 Byte |= (1 << 7);
2223 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2224 } while (Value);
2225}
2226
2227void GenericAsmParser::ParseSleb128(int64_t Value) {
2228 const int64_t Mask = (1 << 7) - 1;
2229 for(;;) {
2230 unsigned Byte = Value & Mask;
2231 Value >>= 7;
2232 bool Done = ((Value == 0 && (Byte & 0x40) == 0) ||
2233 (Value == -1 && (Byte & 0x40) != 0));
2234 if (!Done)
2235 Byte |= (1 << 7);
2236 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2237 if (Done)
2238 break;
2239 }
2240}
2241
2242bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
2243 int64_t Value;
2244 if (getParser().ParseAbsoluteExpression(Value))
2245 return true;
2246
2247 if (getLexer().isNot(AsmToken::EndOfStatement))
2248 return TokError("unexpected token in directive");
2249
2250 if (DirName[1] == 's')
2251 ParseSleb128(Value);
2252 else
2253 ParseUleb128(Value);
2254 return false;
2255}
2256
2257
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002258/// \brief Create an MCAsmParser instance.
2259MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2260 MCContext &C, MCStreamer &Out,
2261 const MCAsmInfo &MAI) {
2262 return new AsmParser(T, SM, C, Out, MAI);
2263}