blob: 8aaee95eda86436964e075bc21b0f684672d80a0 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000028#include "llvm/MC/MCSymbol.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000029#include "llvm/MC/MCDwarf.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000031#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000032#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000033#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000034#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000035using namespace llvm;
36
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000037namespace {
38
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000039/// \brief Helper class for tracking macro definitions.
40struct Macro {
41 StringRef Name;
42 StringRef Body;
43
44public:
45 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
46};
47
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000048/// \brief Helper class for storing information about an active macro
49/// instantiation.
50struct MacroInstantiation {
51 /// The macro being instantiated.
52 const Macro *TheMacro;
53
54 /// The macro instantiation with substitutions.
55 MemoryBuffer *Instantiation;
56
57 /// The location of the instantiation.
58 SMLoc InstantiationLoc;
59
60 /// The location where parsing should resume upon instantiation completion.
61 SMLoc ExitLoc;
62
63public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000064 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
65 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000066};
67
Daniel Dunbaraef87e32010-07-18 18:31:38 +000068/// \brief The concrete assembly parser instance.
69class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000070 friend class GenericAsmParser;
71
Daniel Dunbaraef87e32010-07-18 18:31:38 +000072 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
73 void operator=(const AsmParser &); // DO NOT IMPLEMENT
74private:
75 AsmLexer Lexer;
76 MCContext &Ctx;
77 MCStreamer &Out;
78 SourceMgr &SrcMgr;
79 MCAsmParserExtension *GenericParser;
80 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000081
Daniel Dunbaraef87e32010-07-18 18:31:38 +000082 /// This is the current buffer index we're lexing from as managed by the
83 /// SourceMgr object.
84 int CurBuffer;
85
86 AsmCond TheCondState;
87 std::vector<AsmCond> TheCondStack;
88
89 /// DirectiveMap - This is a table handlers for directives. Each handler is
90 /// invoked after the directive identifier is read and is responsible for
91 /// parsing and validating the rest of the directive. The handler is passed
92 /// in the directive name and the location of the directive keyword.
93 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000094
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000095 /// MacroMap - Map of currently defined macros.
96 StringMap<Macro*> MacroMap;
97
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000098 /// ActiveMacros - Stack of active macro instantiations.
99 std::vector<MacroInstantiation*> ActiveMacros;
100
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000101 /// Boolean tracking whether macro substitution is enabled.
102 unsigned MacrosEnabled : 1;
103
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000104 /// Flag tracking whether any errors have been encountered.
105 unsigned HadError : 1;
106
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000107public:
108 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
109 const MCAsmInfo &MAI);
110 ~AsmParser();
111
112 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
113
114 void AddDirectiveHandler(MCAsmParserExtension *Object,
115 StringRef Directive,
116 DirectiveHandler Handler) {
117 DirectiveMap[Directive] = std::make_pair(Object, Handler);
118 }
119
120public:
121 /// @name MCAsmParser Interface
122 /// {
123
124 virtual SourceMgr &getSourceManager() { return SrcMgr; }
125 virtual MCAsmLexer &getLexer() { return Lexer; }
126 virtual MCContext &getContext() { return Ctx; }
127 virtual MCStreamer &getStreamer() { return Out; }
128
129 virtual void Warning(SMLoc L, const Twine &Meg);
130 virtual bool Error(SMLoc L, const Twine &Msg);
131
132 const AsmToken &Lex();
133
134 bool ParseExpression(const MCExpr *&Res);
135 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
136 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
137 virtual bool ParseAbsoluteExpression(int64_t &Res);
138
139 /// }
140
141private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000142 void CheckForValidSection();
143
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000144 bool ParseStatement();
145
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000146 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
147 void HandleMacroExit();
148
149 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000150 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
151 SrcMgr.PrintMessage(Loc, Msg, Type);
152 }
153
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000154 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
155 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000156
157 /// \brief Reset the current lexer position to that given by \arg Loc. The
158 /// current token is not set; clients should ensure Lex() is called
159 /// subsequently.
160 void JumpToLoc(SMLoc Loc);
161
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000162 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000163
164 /// \brief Parse up to the end of statement and a return the contents from the
165 /// current token until the end of the statement; the current token on exit
166 /// will be either the EndOfStatement or EOF.
167 StringRef ParseStringToEndOfStatement();
168
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000169 bool ParseAssignment(StringRef Name);
170
171 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
172 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
173 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
174
175 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
176 /// and set \arg Res to the identifier contents.
177 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000178
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000179 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000180
181 // ".ascii", ".asciiz", ".string"
182 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000183 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"
Roman Divacky50e7a782010-10-28 16:22:58 +0000188 bool ParseDirectiveSet(StringRef IDVal); // ".set" or ".equ"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000189 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 Dunbar138abae2010-10-16 04:56:42 +0000240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000241
242 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
244 ".macros_on");
245 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
246 ".macros_off");
247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
248 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
249 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000250
251 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
252 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000253 }
254
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000255 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
256 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
257 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000258 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000259
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000260 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000261 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
262 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000263
264 void ParseUleb128(uint64_t Value);
265 void ParseSleb128(int64_t Value);
266 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000267};
268
269}
270
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000271namespace llvm {
272
273extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000274extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000275extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000276
277}
278
Chris Lattneraaec2052010-01-19 19:46:13 +0000279enum { DEFAULT_ADDRSPACE = 0 };
280
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000281AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
282 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000283 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000284 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000285 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000286 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000287
288 // Initialize the generic parser.
289 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000290
291 // Initialize the platform / file format parser.
292 //
293 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
294 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000295 if (_MAI.hasMicrosoftFastStdCallMangling()) {
296 PlatformParser = createCOFFAsmParser();
297 PlatformParser->Initialize(*this);
298 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000299 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000300 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000301 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000302 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000303 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000304 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000305}
306
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000307AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000308 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
309
310 // Destroy any macros.
311 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
312 ie = MacroMap.end(); it != ie; ++it)
313 delete it->getValue();
314
Daniel Dunbare4749702010-07-12 18:12:02 +0000315 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000316 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000317}
318
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000319void AsmParser::PrintMacroInstantiations() {
320 // Print the active macro instantiation stack.
321 for (std::vector<MacroInstantiation*>::const_reverse_iterator
322 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
323 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
324 "note");
325}
326
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000327void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000328 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000329 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000330}
331
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000332bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000333 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000334 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000335 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000336 return true;
337}
338
Sean Callananfd0b0282010-01-21 00:19:58 +0000339bool AsmParser::EnterIncludeFile(const std::string &Filename) {
340 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
341 if (NewBuf == -1)
342 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000343
Sean Callananfd0b0282010-01-21 00:19:58 +0000344 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000345
Sean Callananfd0b0282010-01-21 00:19:58 +0000346 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000347
Sean Callananfd0b0282010-01-21 00:19:58 +0000348 return false;
349}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000350
351void AsmParser::JumpToLoc(SMLoc Loc) {
352 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
353 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
354}
355
Sean Callananfd0b0282010-01-21 00:19:58 +0000356const AsmToken &AsmParser::Lex() {
357 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000358
Sean Callananfd0b0282010-01-21 00:19:58 +0000359 if (tok->is(AsmToken::Eof)) {
360 // If this is the end of an included file, pop the parent file off the
361 // include stack.
362 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
363 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000364 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000365 tok = &Lexer.Lex();
366 }
367 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000368
Sean Callananfd0b0282010-01-21 00:19:58 +0000369 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000370 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000371
Sean Callananfd0b0282010-01-21 00:19:58 +0000372 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000373}
374
Chris Lattner79180e22010-04-05 23:15:42 +0000375bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000376 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000377 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000378 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000379
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000380 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000381 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000382
383 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000384 AsmCond StartingCondState = TheCondState;
385
Chris Lattnerb717fb02009-07-02 21:53:43 +0000386 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000387 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000388 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000389
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000390 // We had an error, validate that one was emitted and recover by skipping to
391 // the next line.
392 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000393 EatToEndOfStatement();
394 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000395
396 if (TheCondState.TheCond != StartingCondState.TheCond ||
397 TheCondState.Ignore != StartingCondState.Ignore)
398 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000399
400 // Check to see there are no empty DwarfFile slots.
401 const std::vector<MCDwarfFile *> &MCDwarfFiles =
402 getContext().getMCDwarfFiles();
403 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000404 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000405 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000406 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000407
Chris Lattner79180e22010-04-05 23:15:42 +0000408 // Finalize the output stream if there are no errors and if the client wants
409 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000410 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000411 Out.Finish();
412
Chris Lattnerb717fb02009-07-02 21:53:43 +0000413 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000414}
415
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000416void AsmParser::CheckForValidSection() {
417 if (!getStreamer().getCurrentSection()) {
418 TokError("expected section directive before assembly directive");
419 Out.SwitchSection(Ctx.getMachOSection(
420 "__TEXT", "__text",
421 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
422 0, SectionKind::getText()));
423 }
424}
425
Chris Lattner2cf5f142009-06-22 01:29:09 +0000426/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
427void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000428 while (Lexer.isNot(AsmToken::EndOfStatement) &&
429 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000430 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000431
Chris Lattner2cf5f142009-06-22 01:29:09 +0000432 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000433 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000434 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000435}
436
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000437StringRef AsmParser::ParseStringToEndOfStatement() {
438 const char *Start = getTok().getLoc().getPointer();
439
440 while (Lexer.isNot(AsmToken::EndOfStatement) &&
441 Lexer.isNot(AsmToken::Eof))
442 Lex();
443
444 const char *End = getTok().getLoc().getPointer();
445 return StringRef(Start, End - Start);
446}
Chris Lattnerc4193832009-06-22 05:51:26 +0000447
Chris Lattner74ec1a32009-06-22 06:32:03 +0000448/// ParseParenExpr - Parse a paren expression and return it.
449/// NOTE: This assumes the leading '(' has already been consumed.
450///
451/// parenexpr ::= expr)
452///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000453bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000454 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000455 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000456 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000457 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000458 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000459 return false;
460}
Chris Lattnerc4193832009-06-22 05:51:26 +0000461
Chris Lattner74ec1a32009-06-22 06:32:03 +0000462/// ParsePrimaryExpr - Parse a primary expression and return it.
463/// primaryexpr ::= (parenexpr
464/// primaryexpr ::= symbol
465/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000466/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000467/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000468bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000469 switch (Lexer.getKind()) {
470 default:
471 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000472 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000473 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000474 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000475 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000476 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000477 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000478 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000479 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000480 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000481 EndLoc = Lexer.getLoc();
482
483 StringRef Identifier;
484 if (ParseIdentifier(Identifier))
485 return false;
486
Daniel Dunbarfffff912009-10-16 01:34:54 +0000487 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000488 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000489 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000490
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000491 // Mark the symbol as used in an expression.
492 Sym->setUsedInExpr(true);
493
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000494 // Lookup the symbol variant if used.
495 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000496 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000497 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000498 if (Variant == MCSymbolRefExpr::VK_Invalid) {
499 Variant = MCSymbolRefExpr::VK_None;
500 TokError("invalid variant '" + Split.second + "'");
501 }
502 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000503
Daniel Dunbarfffff912009-10-16 01:34:54 +0000504 // If this is an absolute variable reference, substitute it now to preserve
505 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000506 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000507 if (Variant)
508 return Error(EndLoc, "unexpected modified on variable reference");
509
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000510 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000511 return false;
512 }
513
514 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000515 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000516 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000517 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000518 case AsmToken::Integer: {
519 SMLoc Loc = getTok().getLoc();
520 int64_t IntVal = getTok().getIntVal();
521 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000522 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000523 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000524 // Look for 'b' or 'f' following an Integer as a directional label
525 if (Lexer.getKind() == AsmToken::Identifier) {
526 StringRef IDVal = getTok().getString();
527 if (IDVal == "f" || IDVal == "b"){
528 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
529 IDVal == "f" ? 1 : 0);
530 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
531 getContext());
532 if(IDVal == "b" && Sym->isUndefined())
533 return Error(Loc, "invalid reference to undefined symbol");
534 EndLoc = Lexer.getLoc();
535 Lex(); // Eat identifier.
536 }
537 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000538 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000539 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000540 case AsmToken::Dot: {
541 // This is a '.' reference, which references the current PC. Emit a
542 // temporary label to the streamer and refer to it.
543 MCSymbol *Sym = Ctx.CreateTempSymbol();
544 Out.EmitLabel(Sym);
545 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
546 EndLoc = Lexer.getLoc();
547 Lex(); // Eat identifier.
548 return false;
549 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000550
Daniel Dunbar3f872332009-07-28 16:08:33 +0000551 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000552 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000553 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000554 case AsmToken::Minus:
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::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000559 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000560 case AsmToken::Plus:
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::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000565 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000566 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000567 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000568 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000569 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000570 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000571 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000572 }
573}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000574
Chris Lattnerb4307b32010-01-15 19:28:38 +0000575bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000576 SMLoc EndLoc;
577 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000578}
579
Daniel Dunbarcceba832010-09-17 02:47:07 +0000580const MCExpr *
581AsmParser::ApplyModifierToExpr(const MCExpr *E,
582 MCSymbolRefExpr::VariantKind Variant) {
583 // Recurse over the given expression, rebuilding it to apply the given variant
584 // if there is exactly one symbol.
585 switch (E->getKind()) {
586 case MCExpr::Target:
587 case MCExpr::Constant:
588 return 0;
589
590 case MCExpr::SymbolRef: {
591 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
592
593 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
594 TokError("invalid variant on expression '" +
595 getTok().getIdentifier() + "' (already modified)");
596 return E;
597 }
598
599 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
600 }
601
602 case MCExpr::Unary: {
603 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
604 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
605 if (!Sub)
606 return 0;
607 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
608 }
609
610 case MCExpr::Binary: {
611 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
612 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
613 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
614
615 if (!LHS && !RHS)
616 return 0;
617
618 if (!LHS) LHS = BE->getLHS();
619 if (!RHS) RHS = BE->getRHS();
620
621 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
622 }
623 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000624
625 assert(0 && "Invalid expression kind!");
626 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000627}
628
Chris Lattner74ec1a32009-06-22 06:32:03 +0000629/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000630///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000631/// expr ::= expr +,- expr -> lowest.
632/// expr ::= expr |,^,&,! expr -> middle.
633/// expr ::= expr *,/,%,<<,>> expr -> highest.
634/// expr ::= primaryexpr
635///
Chris Lattner54482b42010-01-15 19:39:23 +0000636bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000637 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000638 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000639 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
640 return true;
641
Daniel Dunbarcceba832010-09-17 02:47:07 +0000642 // As a special case, we support 'a op b @ modifier' by rewriting the
643 // expression to include the modifier. This is inefficient, but in general we
644 // expect users to use 'a@modifier op b'.
645 if (Lexer.getKind() == AsmToken::At) {
646 Lex();
647
648 if (Lexer.isNot(AsmToken::Identifier))
649 return TokError("unexpected symbol modifier following '@'");
650
651 MCSymbolRefExpr::VariantKind Variant =
652 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
653 if (Variant == MCSymbolRefExpr::VK_Invalid)
654 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
655
656 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
657 if (!ModifiedRes) {
658 return TokError("invalid modifier '" + getTok().getIdentifier() +
659 "' (no symbols present)");
660 return true;
661 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000662
Daniel Dunbarcceba832010-09-17 02:47:07 +0000663 Res = ModifiedRes;
664 Lex();
665 }
666
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000667 // Try to constant fold it up front, if possible.
668 int64_t Value;
669 if (Res->EvaluateAsAbsolute(Value))
670 Res = MCConstantExpr::Create(Value, getContext());
671
672 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000673}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000674
Chris Lattnerb4307b32010-01-15 19:28:38 +0000675bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000676 Res = 0;
677 return ParseParenExpr(Res, EndLoc) ||
678 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000679}
680
Daniel Dunbar475839e2009-06-29 20:37:27 +0000681bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000682 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000683
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000684 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000685 if (ParseExpression(Expr))
686 return true;
687
Daniel Dunbare00b0112009-10-16 01:57:52 +0000688 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000689 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000690
691 return false;
692}
693
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000694static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000695 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000696 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000697 default:
698 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000699
Daniel Dunbarcceba832010-09-17 02:47:07 +0000700 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000701 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000702 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000703 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000704 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000705 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000706 return 1;
707
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000708
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000709 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000710 //
711 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000712 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000713 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000714 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000715 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000716 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000717 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000718 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000719 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000720 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000721
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000722 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000723 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
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000743 // High Intermediate Precedence: +, -
744 case AsmToken::Plus:
745 Kind = MCBinaryExpr::Add;
746 return 4;
747 case AsmToken::Minus:
748 Kind = MCBinaryExpr::Sub;
749 return 4;
750
Daniel Dunbar475839e2009-06-29 20:37:27 +0000751 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000752 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000753 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000754 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000755 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000756 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000757 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000758 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000759 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000760 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000761 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000762 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000763 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000764 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000765 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000766 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000767 }
768}
769
770
771/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
772/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000773bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
774 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000775 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000776 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000777 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000778
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000779 // If the next token is lower precedence than we are allowed to eat, return
780 // successfully with what we ate already.
781 if (TokPrec < Precedence)
782 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000783
Sean Callanan79ed1a82010-01-19 20:22:31 +0000784 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000785
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000786 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000787 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000788 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000789
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000790 // If BinOp binds less tightly with RHS than the operator after RHS, let
791 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000792 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000793 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000794 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000795 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000796 }
797
Daniel Dunbar475839e2009-06-29 20:37:27 +0000798 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000799 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000800 }
801}
802
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000803
804
805
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000806/// ParseStatement:
807/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000808/// ::= Label* Directive ...Operands... EndOfStatement
809/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000810bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000811 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000812 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000813 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000814 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000815 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000816
817 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000818 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000819 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000820 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000821 int64_t LocalLabelVal = -1;
822 // GUESS allow an integer followed by a ':' as a directional local label
823 if (Lexer.is(AsmToken::Integer)) {
824 LocalLabelVal = getTok().getIntVal();
825 if (LocalLabelVal < 0) {
826 if (!TheCondState.Ignore)
827 return TokError("unexpected token at start of statement");
828 IDVal = "";
829 }
830 else {
831 IDVal = getTok().getString();
832 Lex(); // Consume the integer token to be used as an identifier token.
833 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000834 if (!TheCondState.Ignore)
835 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000836 }
837 }
838 }
839 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000840 if (!TheCondState.Ignore)
841 return TokError("unexpected token at start of statement");
842 IDVal = "";
843 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000844
Chris Lattner7834fac2010-04-17 18:14:27 +0000845 // Handle conditional assembly here before checking for skipping. We
846 // have to do this so that .endif isn't skipped in a ".if 0" block for
847 // example.
848 if (IDVal == ".if")
849 return ParseDirectiveIf(IDLoc);
850 if (IDVal == ".elseif")
851 return ParseDirectiveElseIf(IDLoc);
852 if (IDVal == ".else")
853 return ParseDirectiveElse(IDLoc);
854 if (IDVal == ".endif")
855 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000856
Chris Lattner7834fac2010-04-17 18:14:27 +0000857 // If we are in a ".if 0" block, ignore this statement.
858 if (TheCondState.Ignore) {
859 EatToEndOfStatement();
860 return false;
861 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000862
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000863 // FIXME: Recurse on local labels?
864
865 // See what kind of statement we have.
866 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000867 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000868 CheckForValidSection();
869
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000870 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000871 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000872
873 // Diagnose attempt to use a variable as a label.
874 //
875 // FIXME: Diagnostics. Note the location of the definition as a label.
876 // FIXME: This doesn't diagnose assignment to a symbol which has been
877 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000878 MCSymbol *Sym;
879 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000880 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000881 else
882 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000883 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000884 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000885
Daniel Dunbar959fd882009-08-26 22:13:22 +0000886 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000887 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000888
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000889 // Consume any end of statement token, if present, to avoid spurious
890 // AddBlankLine calls().
891 if (Lexer.is(AsmToken::EndOfStatement)) {
892 Lex();
893 if (Lexer.is(AsmToken::Eof))
894 return false;
895 }
896
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000897 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000898 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000899
Daniel Dunbar3f872332009-07-28 16:08:33 +0000900 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000901 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000902 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000903
Daniel Dunbare2ace502009-08-31 08:09:09 +0000904 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000905
906 default: // Normal instruction or directive.
907 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000908 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000909
910 // If macros are enabled, check to see if this is a macro instantiation.
911 if (MacrosEnabled)
912 if (const Macro *M = MacroMap.lookup(IDVal))
913 return HandleMacroEntry(IDVal, IDLoc, M);
914
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000915 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000916 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000917 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +0000918 if (IDVal == ".set" || IDVal == ".equ")
919 return ParseDirectiveSet(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000920
Daniel Dunbara0d14262009-06-24 23:30:00 +0000921 // Data directives
922
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000923 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +0000924 return ParseDirectiveAscii(IDVal, false);
925 if (IDVal == ".asciz" || IDVal == ".string")
926 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000927
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000928 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000929 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000930 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000931 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +0000932 if (IDVal == ".value")
933 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000934 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000935 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000936 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000937 return ParseDirectiveValue(8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000938 if (IDVal == ".single")
939 return ParseDirectiveRealValue(APFloat::IEEEsingle);
940 if (IDVal == ".double")
941 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000942
Eli Friedman5d68ec22010-07-19 04:17:25 +0000943 if (IDVal == ".align") {
944 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
945 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
946 }
947 if (IDVal == ".align32") {
948 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
949 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
950 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000951 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000952 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000953 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000954 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000955 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000956 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000957 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000958 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000959 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000960 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000961 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000962 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
963
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000964 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000965 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000966
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000967 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000968 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000969 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000970 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000971 if (IDVal == ".zero")
972 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000973
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000974 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000975
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000976 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000977 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000978 // ELF only? Should it be here?
979 if (IDVal == ".local")
980 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000981 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000982 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000983 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000984 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000985 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000986 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000987 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000988 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000989 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000990 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000991 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000992 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000993 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000994 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000995 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000996 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000997 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000998 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000999 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001000 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001001 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001002 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001003 if (IDVal == ".weak_def_can_be_hidden")
1004 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001005
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001006 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001007 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001008 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001009 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001010
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001011 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001012 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001013 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001014 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001015
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001016 // Look up the handler in the handler table.
1017 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1018 DirectiveMap.lookup(IDVal);
1019 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001020 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001021
Kevin Enderby9c656452009-09-10 20:51:44 +00001022 // Target hook for parsing target specific directives.
1023 if (!getTargetParser().ParseDirective(ID))
1024 return false;
1025
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001026 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001027 EatToEndOfStatement();
1028 return false;
1029 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001030
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001031 CheckForValidSection();
1032
Chris Lattnera7f13542010-05-19 23:34:33 +00001033 // Canonicalize the opcode to lower case.
1034 SmallString<128> Opcode;
1035 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1036 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001037
Chris Lattner98986712010-01-14 22:21:20 +00001038 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001039 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001040 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001041
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001042 // Dump the parsed representation, if requested.
1043 if (getShowParsedOperands()) {
1044 SmallString<256> Str;
1045 raw_svector_ostream OS(Str);
1046 OS << "parsed instruction: [";
1047 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1048 if (i != 0)
1049 OS << ", ";
1050 ParsedOperands[i]->dump(OS);
1051 }
1052 OS << "]";
1053
1054 PrintMessage(IDLoc, OS.str(), "note");
1055 }
1056
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001057 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001058 if (!HadError)
1059 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1060 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001061
Chris Lattner98986712010-01-14 22:21:20 +00001062 // Free any parsed operands.
1063 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1064 delete ParsedOperands[i];
1065
Chris Lattnercbf8a982010-09-11 16:18:25 +00001066 // Don't skip the rest of the line, the instruction parser is responsible for
1067 // that.
1068 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001069}
Chris Lattner9a023f72009-06-24 04:43:34 +00001070
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001071MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1072 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001073 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1074{
1075 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1076 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001077 SmallString<256> Buf;
1078 raw_svector_ostream OS(Buf);
1079
1080 StringRef Body = M->Body;
1081 while (!Body.empty()) {
1082 // Scan for the next substitution.
1083 std::size_t End = Body.size(), Pos = 0;
1084 for (; Pos != End; ++Pos) {
1085 // Check for a substitution or escape.
1086 if (Body[Pos] != '$' || Pos + 1 == End)
1087 continue;
1088
1089 char Next = Body[Pos + 1];
1090 if (Next == '$' || Next == 'n' || isdigit(Next))
1091 break;
1092 }
1093
1094 // Add the prefix.
1095 OS << Body.slice(0, Pos);
1096
1097 // Check if we reached the end.
1098 if (Pos == End)
1099 break;
1100
1101 switch (Body[Pos+1]) {
1102 // $$ => $
1103 case '$':
1104 OS << '$';
1105 break;
1106
1107 // $n => number of arguments
1108 case 'n':
1109 OS << A.size();
1110 break;
1111
1112 // $[0-9] => argument
1113 default: {
1114 // Missing arguments are ignored.
1115 unsigned Index = Body[Pos+1] - '0';
1116 if (Index >= A.size())
1117 break;
1118
1119 // Otherwise substitute with the token values, with spaces eliminated.
1120 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1121 ie = A[Index].end(); it != ie; ++it)
1122 OS << it->getString();
1123 break;
1124 }
1125 }
1126
1127 // Update the scan point.
1128 Body = Body.substr(Pos + 2);
1129 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001130
1131 // We include the .endmacro in the buffer as our queue to exit the macro
1132 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001133 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001134
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001135 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001136}
1137
1138bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1139 const Macro *M) {
1140 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1141 // this, although we should protect against infinite loops.
1142 if (ActiveMacros.size() == 20)
1143 return TokError("macros cannot be nested more than 20 levels deep");
1144
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001145 // Parse the macro instantiation arguments.
1146 std::vector<std::vector<AsmToken> > MacroArguments;
1147 MacroArguments.push_back(std::vector<AsmToken>());
1148 unsigned ParenLevel = 0;
1149 for (;;) {
1150 if (Lexer.is(AsmToken::Eof))
1151 return TokError("unexpected token in macro instantiation");
1152 if (Lexer.is(AsmToken::EndOfStatement))
1153 break;
1154
1155 // If we aren't inside parentheses and this is a comma, start a new token
1156 // list.
1157 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1158 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001159 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001160 // Adjust the current parentheses level.
1161 if (Lexer.is(AsmToken::LParen))
1162 ++ParenLevel;
1163 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1164 --ParenLevel;
1165
1166 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001167 MacroArguments.back().push_back(getTok());
1168 }
1169 Lex();
1170 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001171
1172 // Create the macro instantiation object and add to the current macro
1173 // instantiation stack.
1174 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001175 getTok().getLoc(),
1176 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001177 ActiveMacros.push_back(MI);
1178
1179 // Jump to the macro instantiation and prime the lexer.
1180 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1181 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1182 Lex();
1183
1184 return false;
1185}
1186
1187void AsmParser::HandleMacroExit() {
1188 // Jump to the EndOfStatement we should return to, and consume it.
1189 JumpToLoc(ActiveMacros.back()->ExitLoc);
1190 Lex();
1191
1192 // Pop the instantiation entry.
1193 delete ActiveMacros.back();
1194 ActiveMacros.pop_back();
1195}
1196
Benjamin Kramer38e59892010-07-14 22:38:02 +00001197bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001198 // FIXME: Use better location, we should use proper tokens.
1199 SMLoc EqualLoc = Lexer.getLoc();
1200
Daniel Dunbar821e3332009-08-31 08:09:28 +00001201 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001202 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001203 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001204
Daniel Dunbar3f872332009-07-28 16:08:33 +00001205 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001206 return TokError("unexpected token in assignment");
1207
1208 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001209 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001210
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001211 // Validate that the LHS is allowed to be a variable (either it has not been
1212 // used as a symbol, or it is an absolute symbol).
1213 MCSymbol *Sym = getContext().LookupSymbol(Name);
1214 if (Sym) {
1215 // Diagnose assignment to a label.
1216 //
1217 // FIXME: Diagnostics. Note the location of the definition as a label.
1218 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001219 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1220 ; // Allow redefinitions of undefined symbols only used in directives.
1221 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001222 return Error(EqualLoc, "redefinition of '" + Name + "'");
1223 else if (!Sym->isVariable())
1224 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001225 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001226 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1227 Name + "'");
1228 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001229 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001230
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001231 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001232
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001233 Sym->setUsedInExpr(true);
1234
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001235 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001236 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001237
1238 return false;
1239}
1240
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001241/// ParseIdentifier:
1242/// ::= identifier
1243/// ::= string
1244bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001245 // The assembler has relaxed rules for accepting identifiers, in particular we
1246 // allow things like '.globl $foo', which would normally be separate
1247 // tokens. At this level, we have already lexed so we cannot (currently)
1248 // handle this as a context dependent token, instead we detect adjacent tokens
1249 // and return the combined identifier.
1250 if (Lexer.is(AsmToken::Dollar)) {
1251 SMLoc DollarLoc = getLexer().getLoc();
1252
1253 // Consume the dollar sign, and check for a following identifier.
1254 Lex();
1255 if (Lexer.isNot(AsmToken::Identifier))
1256 return true;
1257
1258 // We have a '$' followed by an identifier, make sure they are adjacent.
1259 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1260 return true;
1261
1262 // Construct the joined identifier and consume the token.
1263 Res = StringRef(DollarLoc.getPointer(),
1264 getTok().getIdentifier().size() + 1);
1265 Lex();
1266 return false;
1267 }
1268
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001269 if (Lexer.isNot(AsmToken::Identifier) &&
1270 Lexer.isNot(AsmToken::String))
1271 return true;
1272
Sean Callanan18b83232010-01-19 21:44:56 +00001273 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001274
Sean Callanan79ed1a82010-01-19 20:22:31 +00001275 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001276
1277 return false;
1278}
1279
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001280/// ParseDirectiveSet:
1281/// ::= .set identifier ',' expression
Roman Divacky50e7a782010-10-28 16:22:58 +00001282bool AsmParser::ParseDirectiveSet(StringRef IDVal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001283 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001284
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001285 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001286 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001287
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001288 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001289 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001290 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001291
Daniel Dunbare2ace502009-08-31 08:09:09 +00001292 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001293}
1294
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001295bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001296 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001297
1298 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001299 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001300 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1301 if (Str[i] != '\\') {
1302 Data += Str[i];
1303 continue;
1304 }
1305
1306 // Recognize escaped characters. Note that this escape semantics currently
1307 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1308 ++i;
1309 if (i == e)
1310 return TokError("unexpected backslash at end of string");
1311
1312 // Recognize octal sequences.
1313 if ((unsigned) (Str[i] - '0') <= 7) {
1314 // Consume up to three octal characters.
1315 unsigned Value = Str[i] - '0';
1316
1317 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1318 ++i;
1319 Value = Value * 8 + (Str[i] - '0');
1320
1321 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1322 ++i;
1323 Value = Value * 8 + (Str[i] - '0');
1324 }
1325 }
1326
1327 if (Value > 255)
1328 return TokError("invalid octal escape sequence (out of range)");
1329
1330 Data += (unsigned char) Value;
1331 continue;
1332 }
1333
1334 // Otherwise recognize individual escapes.
1335 switch (Str[i]) {
1336 default:
1337 // Just reject invalid escape sequences for now.
1338 return TokError("invalid escape sequence (unrecognized character)");
1339
1340 case 'b': Data += '\b'; break;
1341 case 'f': Data += '\f'; break;
1342 case 'n': Data += '\n'; break;
1343 case 'r': Data += '\r'; break;
1344 case 't': Data += '\t'; break;
1345 case '"': Data += '"'; break;
1346 case '\\': Data += '\\'; break;
1347 }
1348 }
1349
1350 return false;
1351}
1352
Daniel Dunbara0d14262009-06-24 23:30:00 +00001353/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001354/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1355bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001356 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001357 CheckForValidSection();
1358
Daniel Dunbara0d14262009-06-24 23:30:00 +00001359 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001360 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001361 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001362
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001363 std::string Data;
1364 if (ParseEscapedString(Data))
1365 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001366
1367 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001368 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001369 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1370
Sean Callanan79ed1a82010-01-19 20:22:31 +00001371 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001372
1373 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001374 break;
1375
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001376 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001377 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001378 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001379 }
1380 }
1381
Sean Callanan79ed1a82010-01-19 20:22:31 +00001382 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001383 return false;
1384}
1385
1386/// ParseDirectiveValue
1387/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1388bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001389 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001390 CheckForValidSection();
1391
Daniel Dunbara0d14262009-06-24 23:30:00 +00001392 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001393 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001394 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001395 return true;
1396
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001397 // Special case constant expressions to match code generator.
1398 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001399 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001400 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001401 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001402
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001403 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001404 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001405
Daniel Dunbara0d14262009-06-24 23:30:00 +00001406 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001407 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001408 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001409 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001410 }
1411 }
1412
Sean Callanan79ed1a82010-01-19 20:22:31 +00001413 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001414 return false;
1415}
1416
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001417/// ParseDirectiveRealValue
1418/// ::= (.single | .double) [ expression (, expression)* ]
1419bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1420 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1421 CheckForValidSection();
1422
1423 for (;;) {
1424 // We don't truly support arithmetic on floating point expressions, so we
1425 // have to manually parse unary prefixes.
1426 bool IsNeg = false;
1427 if (getLexer().is(AsmToken::Minus)) {
1428 Lex();
1429 IsNeg = true;
1430 } else if (getLexer().is(AsmToken::Plus))
1431 Lex();
1432
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001433 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001434 getLexer().isNot(AsmToken::Real))
1435 return TokError("unexpected token in directive");
1436
1437 // Convert to an APFloat.
1438 APFloat Value(Semantics);
1439 if (Value.convertFromString(getTok().getString(),
1440 APFloat::rmNearestTiesToEven) ==
1441 APFloat::opInvalidOp)
1442 return TokError("invalid floating point literal");
1443 if (IsNeg)
1444 Value.changeSign();
1445
1446 // Consume the numeric token.
1447 Lex();
1448
1449 // Emit the value as an integer.
1450 APInt AsInt = Value.bitcastToAPInt();
1451 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1452 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1453
1454 if (getLexer().is(AsmToken::EndOfStatement))
1455 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001456
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001457 if (getLexer().isNot(AsmToken::Comma))
1458 return TokError("unexpected token in directive");
1459 Lex();
1460 }
1461 }
1462
1463 Lex();
1464 return false;
1465}
1466
Daniel Dunbara0d14262009-06-24 23:30:00 +00001467/// ParseDirectiveSpace
1468/// ::= .space expression [ , expression ]
1469bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001470 CheckForValidSection();
1471
Daniel Dunbara0d14262009-06-24 23:30:00 +00001472 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001473 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001474 return true;
1475
1476 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001477 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1478 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001479 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001480 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001481
Daniel Dunbar475839e2009-06-29 20:37:27 +00001482 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001483 return true;
1484
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001485 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001486 return TokError("unexpected token in '.space' directive");
1487 }
1488
Sean Callanan79ed1a82010-01-19 20:22:31 +00001489 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001490
1491 if (NumBytes <= 0)
1492 return TokError("invalid number of bytes in '.space' directive");
1493
1494 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001495 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001496
1497 return false;
1498}
1499
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001500/// ParseDirectiveZero
1501/// ::= .zero expression
1502bool AsmParser::ParseDirectiveZero() {
1503 CheckForValidSection();
1504
1505 int64_t NumBytes;
1506 if (ParseAbsoluteExpression(NumBytes))
1507 return true;
1508
Rafael Espindolae452b172010-10-05 19:42:57 +00001509 int64_t Val = 0;
1510 if (getLexer().is(AsmToken::Comma)) {
1511 Lex();
1512 if (ParseAbsoluteExpression(Val))
1513 return true;
1514 }
1515
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001516 if (getLexer().isNot(AsmToken::EndOfStatement))
1517 return TokError("unexpected token in '.zero' directive");
1518
1519 Lex();
1520
Rafael Espindolae452b172010-10-05 19:42:57 +00001521 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001522
1523 return false;
1524}
1525
Daniel Dunbara0d14262009-06-24 23:30:00 +00001526/// ParseDirectiveFill
1527/// ::= .fill expression , expression , expression
1528bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001529 CheckForValidSection();
1530
Daniel Dunbara0d14262009-06-24 23:30:00 +00001531 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001532 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001533 return true;
1534
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001535 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001536 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001537 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001538
Daniel Dunbara0d14262009-06-24 23:30:00 +00001539 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001540 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001541 return true;
1542
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001543 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001544 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001545 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001546
Daniel Dunbara0d14262009-06-24 23:30:00 +00001547 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001548 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001549 return true;
1550
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001551 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001552 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001553
Sean Callanan79ed1a82010-01-19 20:22:31 +00001554 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001555
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001556 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1557 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001558
1559 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001560 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001561
1562 return false;
1563}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001564
1565/// ParseDirectiveOrg
1566/// ::= .org expression [ , expression ]
1567bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001568 CheckForValidSection();
1569
Daniel Dunbar821e3332009-08-31 08:09:28 +00001570 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001571 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001572 return true;
1573
1574 // Parse optional fill expression.
1575 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001576 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1577 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001578 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001579 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001580
Daniel Dunbar475839e2009-06-29 20:37:27 +00001581 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001582 return true;
1583
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001584 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001585 return TokError("unexpected token in '.org' directive");
1586 }
1587
Sean Callanan79ed1a82010-01-19 20:22:31 +00001588 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001589
1590 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1591 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001592 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001593
1594 return false;
1595}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001596
1597/// ParseDirectiveAlign
1598/// ::= {.align, ...} expression [ , expression [ , expression ]]
1599bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001600 CheckForValidSection();
1601
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001602 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001603 int64_t Alignment;
1604 if (ParseAbsoluteExpression(Alignment))
1605 return true;
1606
1607 SMLoc MaxBytesLoc;
1608 bool HasFillExpr = false;
1609 int64_t FillExpr = 0;
1610 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001611 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1612 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001613 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001614 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001615
1616 // The fill expression can be omitted while specifying a maximum number of
1617 // alignment bytes, e.g:
1618 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001619 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001620 HasFillExpr = true;
1621 if (ParseAbsoluteExpression(FillExpr))
1622 return true;
1623 }
1624
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001625 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1626 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001627 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001628 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001629
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001630 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001631 if (ParseAbsoluteExpression(MaxBytesToFill))
1632 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001633
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001634 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001635 return TokError("unexpected token in directive");
1636 }
1637 }
1638
Sean Callanan79ed1a82010-01-19 20:22:31 +00001639 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001640
Daniel Dunbar648ac512010-05-17 21:54:30 +00001641 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001642 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001643
1644 // Compute alignment in bytes.
1645 if (IsPow2) {
1646 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001647 if (Alignment >= 32) {
1648 Error(AlignmentLoc, "invalid alignment value");
1649 Alignment = 31;
1650 }
1651
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001652 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001653 }
1654
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001655 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001656 if (MaxBytesLoc.isValid()) {
1657 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001658 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1659 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001660 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001661 }
1662
1663 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001664 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1665 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001666 MaxBytesToFill = 0;
1667 }
1668 }
1669
Daniel Dunbar648ac512010-05-17 21:54:30 +00001670 // Check whether we should use optimal code alignment for this .align
1671 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001672 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001673 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1674 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001675 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001676 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001677 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001678 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1679 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001680 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001681
1682 return false;
1683}
1684
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001685/// ParseDirectiveSymbolAttribute
1686/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001687bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001688 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001689 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001690 StringRef Name;
1691
1692 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001693 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001694
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001695 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001696
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001697 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001698
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001699 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001700 break;
1701
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001702 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001703 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001704 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001705 }
1706 }
1707
Sean Callanan79ed1a82010-01-19 20:22:31 +00001708 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001709 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001710}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001711
1712/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001713/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1714bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001715 CheckForValidSection();
1716
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001717 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001718 StringRef Name;
1719 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001720 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001721
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001722 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001723 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001724
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001725 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001726 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001727 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001728
1729 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001730 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001731 if (ParseAbsoluteExpression(Size))
1732 return true;
1733
1734 int64_t Pow2Alignment = 0;
1735 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001736 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001737 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001738 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001739 if (ParseAbsoluteExpression(Pow2Alignment))
1740 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001741
Chris Lattner258281d2010-01-19 06:22:22 +00001742 // If this target takes alignments in bytes (not log) validate and convert.
1743 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1744 if (!isPowerOf2_64(Pow2Alignment))
1745 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1746 Pow2Alignment = Log2_64(Pow2Alignment);
1747 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001748 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001749
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001750 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001751 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001752
Sean Callanan79ed1a82010-01-19 20:22:31 +00001753 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001754
Chris Lattner1fc3d752009-07-09 17:25:12 +00001755 // NOTE: a size of zero for a .comm should create a undefined symbol
1756 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001757 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001758 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1759 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001760
Eric Christopherc260a3e2010-05-14 01:38:54 +00001761 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001762 // may internally end up wanting an alignment in bytes.
1763 // FIXME: Diagnose overflow.
1764 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001765 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1766 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001767
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001768 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001769 return Error(IDLoc, "invalid symbol redefinition");
1770
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001771 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001772 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001773 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001774 getStreamer().EmitZerofill(Ctx.getMachOSection(
1775 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1776 0, SectionKind::getBSS()),
1777 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001778 return false;
1779 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001780
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001781 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001782 return false;
1783}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001784
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001785/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001786/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001787bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001788 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001789 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001790
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001791 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001792 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001793 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001794
Sean Callanan79ed1a82010-01-19 20:22:31 +00001795 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001796
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001797 if (Str.empty())
1798 Error(Loc, ".abort detected. Assembly stopping.");
1799 else
1800 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001801 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001802
1803 return false;
1804}
Kevin Enderby71148242009-07-14 21:35:03 +00001805
Kevin Enderby1f049b22009-07-14 23:21:55 +00001806/// ParseDirectiveInclude
1807/// ::= .include "filename"
1808bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001809 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001810 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001811
Sean Callanan18b83232010-01-19 21:44:56 +00001812 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001813 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001814 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001815
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001816 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001817 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001818
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001819 // Strip the quotes.
1820 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001821
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001822 // Attempt to switch the lexer to the included file before consuming the end
1823 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001824 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001825 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001826 return true;
1827 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001828
1829 return false;
1830}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001831
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001832/// ParseDirectiveIf
1833/// ::= .if expression
1834bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001835 TheCondStack.push_back(TheCondState);
1836 TheCondState.TheCond = AsmCond::IfCond;
1837 if(TheCondState.Ignore) {
1838 EatToEndOfStatement();
1839 }
1840 else {
1841 int64_t ExprValue;
1842 if (ParseAbsoluteExpression(ExprValue))
1843 return true;
1844
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001845 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001846 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001847
Sean Callanan79ed1a82010-01-19 20:22:31 +00001848 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001849
1850 TheCondState.CondMet = ExprValue;
1851 TheCondState.Ignore = !TheCondState.CondMet;
1852 }
1853
1854 return false;
1855}
1856
1857/// ParseDirectiveElseIf
1858/// ::= .elseif expression
1859bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1860 if (TheCondState.TheCond != AsmCond::IfCond &&
1861 TheCondState.TheCond != AsmCond::ElseIfCond)
1862 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1863 " an .elseif");
1864 TheCondState.TheCond = AsmCond::ElseIfCond;
1865
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001866 bool LastIgnoreState = false;
1867 if (!TheCondStack.empty())
1868 LastIgnoreState = TheCondStack.back().Ignore;
1869 if (LastIgnoreState || TheCondState.CondMet) {
1870 TheCondState.Ignore = true;
1871 EatToEndOfStatement();
1872 }
1873 else {
1874 int64_t ExprValue;
1875 if (ParseAbsoluteExpression(ExprValue))
1876 return true;
1877
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001878 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001879 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001880
Sean Callanan79ed1a82010-01-19 20:22:31 +00001881 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001882 TheCondState.CondMet = ExprValue;
1883 TheCondState.Ignore = !TheCondState.CondMet;
1884 }
1885
1886 return false;
1887}
1888
1889/// ParseDirectiveElse
1890/// ::= .else
1891bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001892 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001893 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001894
Sean Callanan79ed1a82010-01-19 20:22:31 +00001895 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001896
1897 if (TheCondState.TheCond != AsmCond::IfCond &&
1898 TheCondState.TheCond != AsmCond::ElseIfCond)
1899 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1900 ".elseif");
1901 TheCondState.TheCond = AsmCond::ElseCond;
1902 bool LastIgnoreState = false;
1903 if (!TheCondStack.empty())
1904 LastIgnoreState = TheCondStack.back().Ignore;
1905 if (LastIgnoreState || TheCondState.CondMet)
1906 TheCondState.Ignore = true;
1907 else
1908 TheCondState.Ignore = false;
1909
1910 return false;
1911}
1912
1913/// ParseDirectiveEndIf
1914/// ::= .endif
1915bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001916 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001917 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001918
Sean Callanan79ed1a82010-01-19 20:22:31 +00001919 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001920
1921 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1922 TheCondStack.empty())
1923 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1924 ".else");
1925 if (!TheCondStack.empty()) {
1926 TheCondState = TheCondStack.back();
1927 TheCondStack.pop_back();
1928 }
1929
1930 return false;
1931}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001932
1933/// ParseDirectiveFile
1934/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001935bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001936 // FIXME: I'm not sure what this is.
1937 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001938 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001939 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001940 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001941 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001942
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001943 if (FileNumber < 1)
1944 return TokError("file number less than one");
1945 }
1946
Daniel Dunbareceec052010-07-12 17:45:27 +00001947 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001948 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001949
Chris Lattnerd32e8032010-01-25 19:02:58 +00001950 StringRef Filename = getTok().getString();
1951 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001952 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001953
Daniel Dunbareceec052010-07-12 17:45:27 +00001954 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001955 return TokError("unexpected token in '.file' directive");
1956
Chris Lattnerd32e8032010-01-25 19:02:58 +00001957 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001958 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001959 else {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001960 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1961 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001962 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001963 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001964
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001965 return false;
1966}
1967
1968/// ParseDirectiveLine
1969/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001970bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001971 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1972 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001973 return TokError("unexpected token in '.line' directive");
1974
Sean Callanan18b83232010-01-19 21:44:56 +00001975 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001976 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001977 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001978
1979 // FIXME: Do something with the .line.
1980 }
1981
Daniel Dunbareceec052010-07-12 17:45:27 +00001982 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001983 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001984
1985 return false;
1986}
1987
1988
1989/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001990/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001991/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
1992/// The first number is a file number, must have been previously assigned with
1993/// a .file directive, the second number is the line number and optionally the
1994/// third number is a column position (zero if not specified). The remaining
1995/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001996bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001997
Daniel Dunbareceec052010-07-12 17:45:27 +00001998 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001999 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002000 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002001 if (FileNumber < 1)
2002 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002003 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002004 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002005 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002006
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002007 int64_t LineNumber = 0;
2008 if (getLexer().is(AsmToken::Integer)) {
2009 LineNumber = getTok().getIntVal();
2010 if (LineNumber < 1)
2011 return TokError("line number less than one in '.loc' directive");
2012 Lex();
2013 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002014
2015 int64_t ColumnPos = 0;
2016 if (getLexer().is(AsmToken::Integer)) {
2017 ColumnPos = getTok().getIntVal();
2018 if (ColumnPos < 0)
2019 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002020 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002021 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002022
Kevin Enderbyc0957932010-09-30 16:52:03 +00002023 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002024 unsigned Isa = 0;
2025 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2026 for (;;) {
2027 if (getLexer().is(AsmToken::EndOfStatement))
2028 break;
2029
2030 StringRef Name;
2031 SMLoc Loc = getTok().getLoc();
2032 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002033 return TokError("unexpected token in '.loc' directive");
2034
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002035 if (Name == "basic_block")
2036 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2037 else if (Name == "prologue_end")
2038 Flags |= DWARF2_FLAG_PROLOGUE_END;
2039 else if (Name == "epilogue_begin")
2040 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2041 else if (Name == "is_stmt") {
2042 SMLoc Loc = getTok().getLoc();
2043 const MCExpr *Value;
2044 if (getParser().ParseExpression(Value))
2045 return true;
2046 // The expression must be the constant 0 or 1.
2047 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2048 int Value = MCE->getValue();
2049 if (Value == 0)
2050 Flags &= ~DWARF2_FLAG_IS_STMT;
2051 else if (Value == 1)
2052 Flags |= DWARF2_FLAG_IS_STMT;
2053 else
2054 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002055 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002056 else {
2057 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2058 }
2059 }
2060 else if (Name == "isa") {
2061 SMLoc Loc = getTok().getLoc();
2062 const MCExpr *Value;
2063 if (getParser().ParseExpression(Value))
2064 return true;
2065 // The expression must be a constant greater or equal to 0.
2066 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2067 int Value = MCE->getValue();
2068 if (Value < 0)
2069 return Error(Loc, "isa number less than zero");
2070 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002071 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002072 else {
2073 return Error(Loc, "isa number not a constant value");
2074 }
2075 }
2076 else {
2077 return Error(Loc, "unknown sub-directive in '.loc' directive");
2078 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002079
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002080 if (getLexer().is(AsmToken::EndOfStatement))
2081 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002082 }
2083 }
2084
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002085 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,Isa);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002086
2087 return false;
2088}
2089
Daniel Dunbar138abae2010-10-16 04:56:42 +00002090/// ParseDirectiveStabs
2091/// ::= .stabs string, number, number, number
2092bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2093 SMLoc DirectiveLoc) {
2094 return TokError("unsupported directive '" + Directive + "'");
2095}
2096
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002097/// ParseDirectiveMacrosOnOff
2098/// ::= .macros_on
2099/// ::= .macros_off
2100bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2101 SMLoc DirectiveLoc) {
2102 if (getLexer().isNot(AsmToken::EndOfStatement))
2103 return Error(getLexer().getLoc(),
2104 "unexpected token in '" + Directive + "' directive");
2105
2106 getParser().MacrosEnabled = Directive == ".macros_on";
2107
2108 return false;
2109}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002110
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002111/// ParseDirectiveMacro
2112/// ::= .macro name
2113bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2114 SMLoc DirectiveLoc) {
2115 StringRef Name;
2116 if (getParser().ParseIdentifier(Name))
2117 return TokError("expected identifier in directive");
2118
2119 if (getLexer().isNot(AsmToken::EndOfStatement))
2120 return TokError("unexpected token in '.macro' directive");
2121
2122 // Eat the end of statement.
2123 Lex();
2124
2125 AsmToken EndToken, StartToken = getTok();
2126
2127 // Lex the macro definition.
2128 for (;;) {
2129 // Check whether we have reached the end of the file.
2130 if (getLexer().is(AsmToken::Eof))
2131 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2132
2133 // Otherwise, check whether we have reach the .endmacro.
2134 if (getLexer().is(AsmToken::Identifier) &&
2135 (getTok().getIdentifier() == ".endm" ||
2136 getTok().getIdentifier() == ".endmacro")) {
2137 EndToken = getTok();
2138 Lex();
2139 if (getLexer().isNot(AsmToken::EndOfStatement))
2140 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2141 "' directive");
2142 break;
2143 }
2144
2145 // Otherwise, scan til the end of the statement.
2146 getParser().EatToEndOfStatement();
2147 }
2148
2149 if (getParser().MacroMap.lookup(Name)) {
2150 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2151 }
2152
2153 const char *BodyStart = StartToken.getLoc().getPointer();
2154 const char *BodyEnd = EndToken.getLoc().getPointer();
2155 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2156 getParser().MacroMap[Name] = new Macro(Name, Body);
2157 return false;
2158}
2159
2160/// ParseDirectiveEndMacro
2161/// ::= .endm
2162/// ::= .endmacro
2163bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2164 SMLoc DirectiveLoc) {
2165 if (getLexer().isNot(AsmToken::EndOfStatement))
2166 return TokError("unexpected token in '" + Directive + "' directive");
2167
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002168 // If we are inside a macro instantiation, terminate the current
2169 // instantiation.
2170 if (!getParser().ActiveMacros.empty()) {
2171 getParser().HandleMacroExit();
2172 return false;
2173 }
2174
2175 // Otherwise, this .endmacro is a stray entry in the file; well formed
2176 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002177 return TokError("unexpected '" + Directive + "' in file, "
2178 "no current macro definition");
2179}
2180
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002181void GenericAsmParser::ParseUleb128(uint64_t Value) {
2182 const uint64_t Mask = (1 << 7) - 1;
2183 do {
2184 unsigned Byte = Value & Mask;
2185 Value >>= 7;
2186 if (Value) // Not the last one
2187 Byte |= (1 << 7);
2188 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2189 } while (Value);
2190}
2191
2192void GenericAsmParser::ParseSleb128(int64_t Value) {
2193 const int64_t Mask = (1 << 7) - 1;
2194 for(;;) {
2195 unsigned Byte = Value & Mask;
2196 Value >>= 7;
2197 bool Done = ((Value == 0 && (Byte & 0x40) == 0) ||
2198 (Value == -1 && (Byte & 0x40) != 0));
2199 if (!Done)
2200 Byte |= (1 << 7);
2201 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2202 if (Done)
2203 break;
2204 }
2205}
2206
2207bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
2208 int64_t Value;
2209 if (getParser().ParseAbsoluteExpression(Value))
2210 return true;
2211
2212 if (getLexer().isNot(AsmToken::EndOfStatement))
2213 return TokError("unexpected token in directive");
2214
2215 if (DirName[1] == 's')
2216 ParseSleb128(Value);
2217 else
2218 ParseUleb128(Value);
2219 return false;
2220}
2221
2222
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002223/// \brief Create an MCAsmParser instance.
2224MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2225 MCContext &C, MCStreamer &Out,
2226 const MCAsmInfo &MAI) {
2227 return new AsmParser(T, SM, C, Out, MAI);
2228}