blob: aa0f9f0ced9bbe48223b0161f66938d5ad565ee0 [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.
180 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
181 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000182 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000183 bool ParseDirectiveFill(); // ".fill"
184 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000185 bool ParseDirectiveZero(); // ".zero"
Roman Divacky50e7a782010-10-28 16:22:58 +0000186 bool ParseDirectiveSet(StringRef IDVal); // ".set" or ".equ"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000187 bool ParseDirectiveOrg(); // ".org"
188 // ".align{,32}", ".p2align{,w,l}"
189 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
190
191 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
192 /// accepts a single symbol (which should be a label or an external).
193 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
194 bool ParseDirectiveELFType(); // ELF specific ".type"
195
196 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
197
198 bool ParseDirectiveAbort(); // ".abort"
199 bool ParseDirectiveInclude(); // ".include"
200
201 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
202 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
203 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
204 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
205
206 /// ParseEscapedString - Parse the current token as a string which may include
207 /// escaped characters and return the string contents.
208 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000209
210 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
211 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000212};
213
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000214/// \brief Generic implementations of directive handling, etc. which is shared
215/// (or the default, at least) for all assembler parser.
216class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000217 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
218 void AddDirectiveHandler(StringRef Directive) {
219 getParser().AddDirectiveHandler(this, Directive,
220 HandleDirective<GenericAsmParser, Handler>);
221 }
222
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000223public:
224 GenericAsmParser() {}
225
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000226 AsmParser &getParser() {
227 return (AsmParser&) this->MCAsmParserExtension::getParser();
228 }
229
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000230 virtual void Initialize(MCAsmParser &Parser) {
231 // Call the base implementation.
232 this->MCAsmParserExtension::Initialize(Parser);
233
234 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000235 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
236 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
237 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000239
240 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000241 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
242 ".macros_on");
243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
244 ".macros_off");
245 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
246 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000248
249 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
250 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000251 }
252
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000253 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
254 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
255 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000256 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000257
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000258 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000259 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
260 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000261
262 void ParseUleb128(uint64_t Value);
263 void ParseSleb128(int64_t Value);
264 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000265};
266
267}
268
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000269namespace llvm {
270
271extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000272extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000273extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000274
275}
276
Chris Lattneraaec2052010-01-19 19:46:13 +0000277enum { DEFAULT_ADDRSPACE = 0 };
278
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000279AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
280 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000281 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000282 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000283 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000284 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000285
286 // Initialize the generic parser.
287 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000288
289 // Initialize the platform / file format parser.
290 //
291 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
292 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000293 if (_MAI.hasMicrosoftFastStdCallMangling()) {
294 PlatformParser = createCOFFAsmParser();
295 PlatformParser->Initialize(*this);
296 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000297 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000298 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000299 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000300 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000301 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000302 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000303}
304
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000305AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000306 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
307
308 // Destroy any macros.
309 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
310 ie = MacroMap.end(); it != ie; ++it)
311 delete it->getValue();
312
Daniel Dunbare4749702010-07-12 18:12:02 +0000313 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000314 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000315}
316
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000317void AsmParser::PrintMacroInstantiations() {
318 // Print the active macro instantiation stack.
319 for (std::vector<MacroInstantiation*>::const_reverse_iterator
320 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
321 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
322 "note");
323}
324
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000325void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000326 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000327 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000328}
329
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000330bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000331 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000332 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000333 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000334 return true;
335}
336
Sean Callananfd0b0282010-01-21 00:19:58 +0000337bool AsmParser::EnterIncludeFile(const std::string &Filename) {
338 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
339 if (NewBuf == -1)
340 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000341
Sean Callananfd0b0282010-01-21 00:19:58 +0000342 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000343
Sean Callananfd0b0282010-01-21 00:19:58 +0000344 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000345
Sean Callananfd0b0282010-01-21 00:19:58 +0000346 return false;
347}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000348
349void AsmParser::JumpToLoc(SMLoc Loc) {
350 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
351 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
352}
353
Sean Callananfd0b0282010-01-21 00:19:58 +0000354const AsmToken &AsmParser::Lex() {
355 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000356
Sean Callananfd0b0282010-01-21 00:19:58 +0000357 if (tok->is(AsmToken::Eof)) {
358 // If this is the end of an included file, pop the parent file off the
359 // include stack.
360 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
361 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000362 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000363 tok = &Lexer.Lex();
364 }
365 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000366
Sean Callananfd0b0282010-01-21 00:19:58 +0000367 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000368 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000369
Sean Callananfd0b0282010-01-21 00:19:58 +0000370 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000371}
372
Chris Lattner79180e22010-04-05 23:15:42 +0000373bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000374 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000375 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000376 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000377
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000378 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000379 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000380
381 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000382 AsmCond StartingCondState = TheCondState;
383
Chris Lattnerb717fb02009-07-02 21:53:43 +0000384 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000385 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000386 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000387
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000388 // We had an error, validate that one was emitted and recover by skipping to
389 // the next line.
390 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000391 EatToEndOfStatement();
392 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000393
394 if (TheCondState.TheCond != StartingCondState.TheCond ||
395 TheCondState.Ignore != StartingCondState.Ignore)
396 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000397
398 // Check to see there are no empty DwarfFile slots.
399 const std::vector<MCDwarfFile *> &MCDwarfFiles =
400 getContext().getMCDwarfFiles();
401 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000402 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000403 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000404 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000405
Chris Lattner79180e22010-04-05 23:15:42 +0000406 // Finalize the output stream if there are no errors and if the client wants
407 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000408 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000409 Out.Finish();
410
Chris Lattnerb717fb02009-07-02 21:53:43 +0000411 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000412}
413
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000414void AsmParser::CheckForValidSection() {
415 if (!getStreamer().getCurrentSection()) {
416 TokError("expected section directive before assembly directive");
417 Out.SwitchSection(Ctx.getMachOSection(
418 "__TEXT", "__text",
419 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
420 0, SectionKind::getText()));
421 }
422}
423
Chris Lattner2cf5f142009-06-22 01:29:09 +0000424/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
425void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000426 while (Lexer.isNot(AsmToken::EndOfStatement) &&
427 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000428 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000429
Chris Lattner2cf5f142009-06-22 01:29:09 +0000430 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000431 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000432 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000433}
434
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000435StringRef AsmParser::ParseStringToEndOfStatement() {
436 const char *Start = getTok().getLoc().getPointer();
437
438 while (Lexer.isNot(AsmToken::EndOfStatement) &&
439 Lexer.isNot(AsmToken::Eof))
440 Lex();
441
442 const char *End = getTok().getLoc().getPointer();
443 return StringRef(Start, End - Start);
444}
Chris Lattnerc4193832009-06-22 05:51:26 +0000445
Chris Lattner74ec1a32009-06-22 06:32:03 +0000446/// ParseParenExpr - Parse a paren expression and return it.
447/// NOTE: This assumes the leading '(' has already been consumed.
448///
449/// parenexpr ::= expr)
450///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000451bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000452 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000453 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000454 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000455 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000456 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000457 return false;
458}
Chris Lattnerc4193832009-06-22 05:51:26 +0000459
Chris Lattner74ec1a32009-06-22 06:32:03 +0000460/// ParsePrimaryExpr - Parse a primary expression and return it.
461/// primaryexpr ::= (parenexpr
462/// primaryexpr ::= symbol
463/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000464/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000465/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000466bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000467 switch (Lexer.getKind()) {
468 default:
469 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000470 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000471 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000472 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000473 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000474 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000475 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000476 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000477 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000478 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000479 EndLoc = Lexer.getLoc();
480
481 StringRef Identifier;
482 if (ParseIdentifier(Identifier))
483 return false;
484
Daniel Dunbarfffff912009-10-16 01:34:54 +0000485 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000486 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000487 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000488
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000489 // Mark the symbol as used in an expression.
490 Sym->setUsedInExpr(true);
491
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000492 // Lookup the symbol variant if used.
493 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000494 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000495 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000496 if (Variant == MCSymbolRefExpr::VK_Invalid) {
497 Variant = MCSymbolRefExpr::VK_None;
498 TokError("invalid variant '" + Split.second + "'");
499 }
500 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000501
Daniel Dunbarfffff912009-10-16 01:34:54 +0000502 // If this is an absolute variable reference, substitute it now to preserve
503 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000504 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000505 if (Variant)
506 return Error(EndLoc, "unexpected modified on variable reference");
507
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000508 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000509 return false;
510 }
511
512 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000513 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000514 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000515 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000516 case AsmToken::Integer: {
517 SMLoc Loc = getTok().getLoc();
518 int64_t IntVal = getTok().getIntVal();
519 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000520 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000521 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000522 // Look for 'b' or 'f' following an Integer as a directional label
523 if (Lexer.getKind() == AsmToken::Identifier) {
524 StringRef IDVal = getTok().getString();
525 if (IDVal == "f" || IDVal == "b"){
526 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
527 IDVal == "f" ? 1 : 0);
528 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
529 getContext());
530 if(IDVal == "b" && Sym->isUndefined())
531 return Error(Loc, "invalid reference to undefined symbol");
532 EndLoc = Lexer.getLoc();
533 Lex(); // Eat identifier.
534 }
535 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000536 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000537 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000538 case AsmToken::Dot: {
539 // This is a '.' reference, which references the current PC. Emit a
540 // temporary label to the streamer and refer to it.
541 MCSymbol *Sym = Ctx.CreateTempSymbol();
542 Out.EmitLabel(Sym);
543 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
544 EndLoc = Lexer.getLoc();
545 Lex(); // Eat identifier.
546 return false;
547 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000548
Daniel Dunbar3f872332009-07-28 16:08:33 +0000549 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000550 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000551 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000552 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000553 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000554 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000555 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000556 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000557 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000558 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000559 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000560 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000561 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000562 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000563 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000564 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000565 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000566 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000567 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000568 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000569 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000570 }
571}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000572
Chris Lattnerb4307b32010-01-15 19:28:38 +0000573bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000574 SMLoc EndLoc;
575 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000576}
577
Daniel Dunbarcceba832010-09-17 02:47:07 +0000578const MCExpr *
579AsmParser::ApplyModifierToExpr(const MCExpr *E,
580 MCSymbolRefExpr::VariantKind Variant) {
581 // Recurse over the given expression, rebuilding it to apply the given variant
582 // if there is exactly one symbol.
583 switch (E->getKind()) {
584 case MCExpr::Target:
585 case MCExpr::Constant:
586 return 0;
587
588 case MCExpr::SymbolRef: {
589 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
590
591 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
592 TokError("invalid variant on expression '" +
593 getTok().getIdentifier() + "' (already modified)");
594 return E;
595 }
596
597 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
598 }
599
600 case MCExpr::Unary: {
601 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
602 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
603 if (!Sub)
604 return 0;
605 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
606 }
607
608 case MCExpr::Binary: {
609 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
610 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
611 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
612
613 if (!LHS && !RHS)
614 return 0;
615
616 if (!LHS) LHS = BE->getLHS();
617 if (!RHS) RHS = BE->getRHS();
618
619 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
620 }
621 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000622
623 assert(0 && "Invalid expression kind!");
624 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000625}
626
Chris Lattner74ec1a32009-06-22 06:32:03 +0000627/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000628///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000629/// expr ::= expr +,- expr -> lowest.
630/// expr ::= expr |,^,&,! expr -> middle.
631/// expr ::= expr *,/,%,<<,>> expr -> highest.
632/// expr ::= primaryexpr
633///
Chris Lattner54482b42010-01-15 19:39:23 +0000634bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000635 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000636 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000637 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
638 return true;
639
Daniel Dunbarcceba832010-09-17 02:47:07 +0000640 // As a special case, we support 'a op b @ modifier' by rewriting the
641 // expression to include the modifier. This is inefficient, but in general we
642 // expect users to use 'a@modifier op b'.
643 if (Lexer.getKind() == AsmToken::At) {
644 Lex();
645
646 if (Lexer.isNot(AsmToken::Identifier))
647 return TokError("unexpected symbol modifier following '@'");
648
649 MCSymbolRefExpr::VariantKind Variant =
650 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
651 if (Variant == MCSymbolRefExpr::VK_Invalid)
652 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
653
654 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
655 if (!ModifiedRes) {
656 return TokError("invalid modifier '" + getTok().getIdentifier() +
657 "' (no symbols present)");
658 return true;
659 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000660
Daniel Dunbarcceba832010-09-17 02:47:07 +0000661 Res = ModifiedRes;
662 Lex();
663 }
664
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000665 // Try to constant fold it up front, if possible.
666 int64_t Value;
667 if (Res->EvaluateAsAbsolute(Value))
668 Res = MCConstantExpr::Create(Value, getContext());
669
670 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000671}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000672
Chris Lattnerb4307b32010-01-15 19:28:38 +0000673bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000674 Res = 0;
675 return ParseParenExpr(Res, EndLoc) ||
676 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000677}
678
Daniel Dunbar475839e2009-06-29 20:37:27 +0000679bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000680 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000681
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000682 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000683 if (ParseExpression(Expr))
684 return true;
685
Daniel Dunbare00b0112009-10-16 01:57:52 +0000686 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000687 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000688
689 return false;
690}
691
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000692static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000693 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000694 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000695 default:
696 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000697
Daniel Dunbarcceba832010-09-17 02:47:07 +0000698 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000699 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000700 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000701 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000702 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000703 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000704 return 1;
705
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000706
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000707 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000708 //
709 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000710 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000711 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000712 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000713 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000714 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000715 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000716 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000717 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000718 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000719
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000720 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000721 case AsmToken::EqualEqual:
722 Kind = MCBinaryExpr::EQ;
723 return 3;
724 case AsmToken::ExclaimEqual:
725 case AsmToken::LessGreater:
726 Kind = MCBinaryExpr::NE;
727 return 3;
728 case AsmToken::Less:
729 Kind = MCBinaryExpr::LT;
730 return 3;
731 case AsmToken::LessEqual:
732 Kind = MCBinaryExpr::LTE;
733 return 3;
734 case AsmToken::Greater:
735 Kind = MCBinaryExpr::GT;
736 return 3;
737 case AsmToken::GreaterEqual:
738 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000739 return 3;
740
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000741 // High Intermediate Precedence: +, -
742 case AsmToken::Plus:
743 Kind = MCBinaryExpr::Add;
744 return 4;
745 case AsmToken::Minus:
746 Kind = MCBinaryExpr::Sub;
747 return 4;
748
Daniel Dunbar475839e2009-06-29 20:37:27 +0000749 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000750 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000751 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000752 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000753 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000754 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000755 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000756 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000757 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000758 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000759 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000760 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000761 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000762 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000763 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000764 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000765 }
766}
767
768
769/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
770/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000771bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
772 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000773 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000774 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000775 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000776
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000777 // If the next token is lower precedence than we are allowed to eat, return
778 // successfully with what we ate already.
779 if (TokPrec < Precedence)
780 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000781
Sean Callanan79ed1a82010-01-19 20:22:31 +0000782 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000783
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000784 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000785 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000786 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000787
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000788 // If BinOp binds less tightly with RHS than the operator after RHS, let
789 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000790 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000791 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000792 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000793 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000794 }
795
Daniel Dunbar475839e2009-06-29 20:37:27 +0000796 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000797 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000798 }
799}
800
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000801
802
803
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000804/// ParseStatement:
805/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000806/// ::= Label* Directive ...Operands... EndOfStatement
807/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000808bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000809 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000810 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000811 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000812 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000813 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000814
815 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000816 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000817 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000818 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000819 int64_t LocalLabelVal = -1;
820 // GUESS allow an integer followed by a ':' as a directional local label
821 if (Lexer.is(AsmToken::Integer)) {
822 LocalLabelVal = getTok().getIntVal();
823 if (LocalLabelVal < 0) {
824 if (!TheCondState.Ignore)
825 return TokError("unexpected token at start of statement");
826 IDVal = "";
827 }
828 else {
829 IDVal = getTok().getString();
830 Lex(); // Consume the integer token to be used as an identifier token.
831 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000832 if (!TheCondState.Ignore)
833 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000834 }
835 }
836 }
837 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000838 if (!TheCondState.Ignore)
839 return TokError("unexpected token at start of statement");
840 IDVal = "";
841 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000842
Chris Lattner7834fac2010-04-17 18:14:27 +0000843 // Handle conditional assembly here before checking for skipping. We
844 // have to do this so that .endif isn't skipped in a ".if 0" block for
845 // example.
846 if (IDVal == ".if")
847 return ParseDirectiveIf(IDLoc);
848 if (IDVal == ".elseif")
849 return ParseDirectiveElseIf(IDLoc);
850 if (IDVal == ".else")
851 return ParseDirectiveElse(IDLoc);
852 if (IDVal == ".endif")
853 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000854
Chris Lattner7834fac2010-04-17 18:14:27 +0000855 // If we are in a ".if 0" block, ignore this statement.
856 if (TheCondState.Ignore) {
857 EatToEndOfStatement();
858 return false;
859 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000860
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000861 // FIXME: Recurse on local labels?
862
863 // See what kind of statement we have.
864 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000865 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000866 CheckForValidSection();
867
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000868 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000869 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000870
871 // Diagnose attempt to use a variable as a label.
872 //
873 // FIXME: Diagnostics. Note the location of the definition as a label.
874 // FIXME: This doesn't diagnose assignment to a symbol which has been
875 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000876 MCSymbol *Sym;
877 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000878 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000879 else
880 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000881 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000882 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000883
Daniel Dunbar959fd882009-08-26 22:13:22 +0000884 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000885 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000886
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000887 // Consume any end of statement token, if present, to avoid spurious
888 // AddBlankLine calls().
889 if (Lexer.is(AsmToken::EndOfStatement)) {
890 Lex();
891 if (Lexer.is(AsmToken::Eof))
892 return false;
893 }
894
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000895 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000896 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000897
Daniel Dunbar3f872332009-07-28 16:08:33 +0000898 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000899 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000900 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000901
Daniel Dunbare2ace502009-08-31 08:09:09 +0000902 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000903
904 default: // Normal instruction or directive.
905 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000906 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000907
908 // If macros are enabled, check to see if this is a macro instantiation.
909 if (MacrosEnabled)
910 if (const Macro *M = MacroMap.lookup(IDVal))
911 return HandleMacroEntry(IDVal, IDLoc, M);
912
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000913 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000914 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000915 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +0000916 if (IDVal == ".set" || IDVal == ".equ")
917 return ParseDirectiveSet(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000918
Daniel Dunbara0d14262009-06-24 23:30:00 +0000919 // Data directives
920
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000921 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000922 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000923 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000924 return ParseDirectiveAscii(true);
925
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000926 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000927 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000928 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000929 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000930 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000931 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000932 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000933 return ParseDirectiveValue(8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000934 if (IDVal == ".single")
935 return ParseDirectiveRealValue(APFloat::IEEEsingle);
936 if (IDVal == ".double")
937 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000938
Eli Friedman5d68ec22010-07-19 04:17:25 +0000939 if (IDVal == ".align") {
940 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
941 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
942 }
943 if (IDVal == ".align32") {
944 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
945 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
946 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000947 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000948 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000949 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000950 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000951 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000952 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000953 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000954 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000955 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000956 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000957 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000958 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
959
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000960 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000961 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000962
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000963 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000964 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000965 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000966 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000967 if (IDVal == ".zero")
968 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000969
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000970 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000971
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000972 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000973 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000974 // ELF only? Should it be here?
975 if (IDVal == ".local")
976 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000977 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000978 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000979 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000980 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000981 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000982 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000983 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000984 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000985 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000986 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000987 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000988 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000989 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000990 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000991 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000992 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000993 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000994 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000995 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000996 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000997 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000998 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000999 if (IDVal == ".weak_def_can_be_hidden")
1000 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001001
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001002 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001003 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001004 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001005 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001006
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001007 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001008 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001009 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001010 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001011
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001012 // Look up the handler in the handler table.
1013 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1014 DirectiveMap.lookup(IDVal);
1015 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001016 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001017
Kevin Enderby9c656452009-09-10 20:51:44 +00001018 // Target hook for parsing target specific directives.
1019 if (!getTargetParser().ParseDirective(ID))
1020 return false;
1021
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001022 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001023 EatToEndOfStatement();
1024 return false;
1025 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001026
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001027 CheckForValidSection();
1028
Chris Lattnera7f13542010-05-19 23:34:33 +00001029 // Canonicalize the opcode to lower case.
1030 SmallString<128> Opcode;
1031 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1032 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001033
Chris Lattner98986712010-01-14 22:21:20 +00001034 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001035 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001036 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001037
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001038 // Dump the parsed representation, if requested.
1039 if (getShowParsedOperands()) {
1040 SmallString<256> Str;
1041 raw_svector_ostream OS(Str);
1042 OS << "parsed instruction: [";
1043 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1044 if (i != 0)
1045 OS << ", ";
1046 ParsedOperands[i]->dump(OS);
1047 }
1048 OS << "]";
1049
1050 PrintMessage(IDLoc, OS.str(), "note");
1051 }
1052
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001053 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001054 if (!HadError)
1055 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1056 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001057
Chris Lattner98986712010-01-14 22:21:20 +00001058 // Free any parsed operands.
1059 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1060 delete ParsedOperands[i];
1061
Chris Lattnercbf8a982010-09-11 16:18:25 +00001062 // Don't skip the rest of the line, the instruction parser is responsible for
1063 // that.
1064 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001065}
Chris Lattner9a023f72009-06-24 04:43:34 +00001066
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001067MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1068 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001069 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1070{
1071 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1072 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001073 SmallString<256> Buf;
1074 raw_svector_ostream OS(Buf);
1075
1076 StringRef Body = M->Body;
1077 while (!Body.empty()) {
1078 // Scan for the next substitution.
1079 std::size_t End = Body.size(), Pos = 0;
1080 for (; Pos != End; ++Pos) {
1081 // Check for a substitution or escape.
1082 if (Body[Pos] != '$' || Pos + 1 == End)
1083 continue;
1084
1085 char Next = Body[Pos + 1];
1086 if (Next == '$' || Next == 'n' || isdigit(Next))
1087 break;
1088 }
1089
1090 // Add the prefix.
1091 OS << Body.slice(0, Pos);
1092
1093 // Check if we reached the end.
1094 if (Pos == End)
1095 break;
1096
1097 switch (Body[Pos+1]) {
1098 // $$ => $
1099 case '$':
1100 OS << '$';
1101 break;
1102
1103 // $n => number of arguments
1104 case 'n':
1105 OS << A.size();
1106 break;
1107
1108 // $[0-9] => argument
1109 default: {
1110 // Missing arguments are ignored.
1111 unsigned Index = Body[Pos+1] - '0';
1112 if (Index >= A.size())
1113 break;
1114
1115 // Otherwise substitute with the token values, with spaces eliminated.
1116 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1117 ie = A[Index].end(); it != ie; ++it)
1118 OS << it->getString();
1119 break;
1120 }
1121 }
1122
1123 // Update the scan point.
1124 Body = Body.substr(Pos + 2);
1125 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001126
1127 // We include the .endmacro in the buffer as our queue to exit the macro
1128 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001129 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001130
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001131 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001132}
1133
1134bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1135 const Macro *M) {
1136 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1137 // this, although we should protect against infinite loops.
1138 if (ActiveMacros.size() == 20)
1139 return TokError("macros cannot be nested more than 20 levels deep");
1140
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001141 // Parse the macro instantiation arguments.
1142 std::vector<std::vector<AsmToken> > MacroArguments;
1143 MacroArguments.push_back(std::vector<AsmToken>());
1144 unsigned ParenLevel = 0;
1145 for (;;) {
1146 if (Lexer.is(AsmToken::Eof))
1147 return TokError("unexpected token in macro instantiation");
1148 if (Lexer.is(AsmToken::EndOfStatement))
1149 break;
1150
1151 // If we aren't inside parentheses and this is a comma, start a new token
1152 // list.
1153 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1154 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001155 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001156 // Adjust the current parentheses level.
1157 if (Lexer.is(AsmToken::LParen))
1158 ++ParenLevel;
1159 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1160 --ParenLevel;
1161
1162 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001163 MacroArguments.back().push_back(getTok());
1164 }
1165 Lex();
1166 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001167
1168 // Create the macro instantiation object and add to the current macro
1169 // instantiation stack.
1170 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001171 getTok().getLoc(),
1172 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001173 ActiveMacros.push_back(MI);
1174
1175 // Jump to the macro instantiation and prime the lexer.
1176 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1177 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1178 Lex();
1179
1180 return false;
1181}
1182
1183void AsmParser::HandleMacroExit() {
1184 // Jump to the EndOfStatement we should return to, and consume it.
1185 JumpToLoc(ActiveMacros.back()->ExitLoc);
1186 Lex();
1187
1188 // Pop the instantiation entry.
1189 delete ActiveMacros.back();
1190 ActiveMacros.pop_back();
1191}
1192
Benjamin Kramer38e59892010-07-14 22:38:02 +00001193bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001194 // FIXME: Use better location, we should use proper tokens.
1195 SMLoc EqualLoc = Lexer.getLoc();
1196
Daniel Dunbar821e3332009-08-31 08:09:28 +00001197 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001198 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001199 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001200
Daniel Dunbar3f872332009-07-28 16:08:33 +00001201 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001202 return TokError("unexpected token in assignment");
1203
1204 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001205 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001206
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001207 // Validate that the LHS is allowed to be a variable (either it has not been
1208 // used as a symbol, or it is an absolute symbol).
1209 MCSymbol *Sym = getContext().LookupSymbol(Name);
1210 if (Sym) {
1211 // Diagnose assignment to a label.
1212 //
1213 // FIXME: Diagnostics. Note the location of the definition as a label.
1214 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001215 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1216 ; // Allow redefinitions of undefined symbols only used in directives.
1217 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001218 return Error(EqualLoc, "redefinition of '" + Name + "'");
1219 else if (!Sym->isVariable())
1220 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001221 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001222 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1223 Name + "'");
1224 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001225 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001226
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001227 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001228
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001229 Sym->setUsedInExpr(true);
1230
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001231 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001232 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001233
1234 return false;
1235}
1236
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001237/// ParseIdentifier:
1238/// ::= identifier
1239/// ::= string
1240bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001241 // The assembler has relaxed rules for accepting identifiers, in particular we
1242 // allow things like '.globl $foo', which would normally be separate
1243 // tokens. At this level, we have already lexed so we cannot (currently)
1244 // handle this as a context dependent token, instead we detect adjacent tokens
1245 // and return the combined identifier.
1246 if (Lexer.is(AsmToken::Dollar)) {
1247 SMLoc DollarLoc = getLexer().getLoc();
1248
1249 // Consume the dollar sign, and check for a following identifier.
1250 Lex();
1251 if (Lexer.isNot(AsmToken::Identifier))
1252 return true;
1253
1254 // We have a '$' followed by an identifier, make sure they are adjacent.
1255 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1256 return true;
1257
1258 // Construct the joined identifier and consume the token.
1259 Res = StringRef(DollarLoc.getPointer(),
1260 getTok().getIdentifier().size() + 1);
1261 Lex();
1262 return false;
1263 }
1264
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001265 if (Lexer.isNot(AsmToken::Identifier) &&
1266 Lexer.isNot(AsmToken::String))
1267 return true;
1268
Sean Callanan18b83232010-01-19 21:44:56 +00001269 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001270
Sean Callanan79ed1a82010-01-19 20:22:31 +00001271 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001272
1273 return false;
1274}
1275
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001276/// ParseDirectiveSet:
1277/// ::= .set identifier ',' expression
Roman Divacky50e7a782010-10-28 16:22:58 +00001278bool AsmParser::ParseDirectiveSet(StringRef IDVal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001279 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001280
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001281 if (ParseIdentifier(Name))
Roman Divacky50e7a782010-10-28 16:22:58 +00001282 return TokError("expected identifier after '" + Twine(IDVal.str()) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001283
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001284 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky50e7a782010-10-28 16:22:58 +00001285 return TokError("unexpected token in '" + Twine(IDVal.str()) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001286 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001287
Daniel Dunbare2ace502009-08-31 08:09:09 +00001288 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001289}
1290
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001291bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001292 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001293
1294 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001295 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001296 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1297 if (Str[i] != '\\') {
1298 Data += Str[i];
1299 continue;
1300 }
1301
1302 // Recognize escaped characters. Note that this escape semantics currently
1303 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1304 ++i;
1305 if (i == e)
1306 return TokError("unexpected backslash at end of string");
1307
1308 // Recognize octal sequences.
1309 if ((unsigned) (Str[i] - '0') <= 7) {
1310 // Consume up to three octal characters.
1311 unsigned Value = Str[i] - '0';
1312
1313 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1314 ++i;
1315 Value = Value * 8 + (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 }
1322
1323 if (Value > 255)
1324 return TokError("invalid octal escape sequence (out of range)");
1325
1326 Data += (unsigned char) Value;
1327 continue;
1328 }
1329
1330 // Otherwise recognize individual escapes.
1331 switch (Str[i]) {
1332 default:
1333 // Just reject invalid escape sequences for now.
1334 return TokError("invalid escape sequence (unrecognized character)");
1335
1336 case 'b': Data += '\b'; break;
1337 case 'f': Data += '\f'; break;
1338 case 'n': Data += '\n'; break;
1339 case 'r': Data += '\r'; break;
1340 case 't': Data += '\t'; break;
1341 case '"': Data += '"'; break;
1342 case '\\': Data += '\\'; break;
1343 }
1344 }
1345
1346 return false;
1347}
1348
Daniel Dunbara0d14262009-06-24 23:30:00 +00001349/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001350/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001351bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001352 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001353 CheckForValidSection();
1354
Daniel Dunbara0d14262009-06-24 23:30:00 +00001355 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001356 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001357 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001358
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001359 std::string Data;
1360 if (ParseEscapedString(Data))
1361 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001362
1363 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001364 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001365 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1366
Sean Callanan79ed1a82010-01-19 20:22:31 +00001367 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001368
1369 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001370 break;
1371
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001372 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001373 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001374 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001375 }
1376 }
1377
Sean Callanan79ed1a82010-01-19 20:22:31 +00001378 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001379 return false;
1380}
1381
1382/// ParseDirectiveValue
1383/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1384bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001385 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001386 CheckForValidSection();
1387
Daniel Dunbara0d14262009-06-24 23:30:00 +00001388 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001389 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001390 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001391 return true;
1392
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001393 // Special case constant expressions to match code generator.
1394 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001395 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001396 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001397 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001398
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001399 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001400 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001401
Daniel Dunbara0d14262009-06-24 23:30:00 +00001402 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001403 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001404 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001405 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001406 }
1407 }
1408
Sean Callanan79ed1a82010-01-19 20:22:31 +00001409 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001410 return false;
1411}
1412
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001413/// ParseDirectiveRealValue
1414/// ::= (.single | .double) [ expression (, expression)* ]
1415bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1416 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1417 CheckForValidSection();
1418
1419 for (;;) {
1420 // We don't truly support arithmetic on floating point expressions, so we
1421 // have to manually parse unary prefixes.
1422 bool IsNeg = false;
1423 if (getLexer().is(AsmToken::Minus)) {
1424 Lex();
1425 IsNeg = true;
1426 } else if (getLexer().is(AsmToken::Plus))
1427 Lex();
1428
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001429 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001430 getLexer().isNot(AsmToken::Real))
1431 return TokError("unexpected token in directive");
1432
1433 // Convert to an APFloat.
1434 APFloat Value(Semantics);
1435 if (Value.convertFromString(getTok().getString(),
1436 APFloat::rmNearestTiesToEven) ==
1437 APFloat::opInvalidOp)
1438 return TokError("invalid floating point literal");
1439 if (IsNeg)
1440 Value.changeSign();
1441
1442 // Consume the numeric token.
1443 Lex();
1444
1445 // Emit the value as an integer.
1446 APInt AsInt = Value.bitcastToAPInt();
1447 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1448 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1449
1450 if (getLexer().is(AsmToken::EndOfStatement))
1451 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001452
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001453 if (getLexer().isNot(AsmToken::Comma))
1454 return TokError("unexpected token in directive");
1455 Lex();
1456 }
1457 }
1458
1459 Lex();
1460 return false;
1461}
1462
Daniel Dunbara0d14262009-06-24 23:30:00 +00001463/// ParseDirectiveSpace
1464/// ::= .space expression [ , expression ]
1465bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001466 CheckForValidSection();
1467
Daniel Dunbara0d14262009-06-24 23:30:00 +00001468 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001469 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001470 return true;
1471
1472 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001473 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1474 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001475 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001476 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001477
Daniel Dunbar475839e2009-06-29 20:37:27 +00001478 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001479 return true;
1480
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001481 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001482 return TokError("unexpected token in '.space' directive");
1483 }
1484
Sean Callanan79ed1a82010-01-19 20:22:31 +00001485 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001486
1487 if (NumBytes <= 0)
1488 return TokError("invalid number of bytes in '.space' directive");
1489
1490 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001491 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001492
1493 return false;
1494}
1495
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001496/// ParseDirectiveZero
1497/// ::= .zero expression
1498bool AsmParser::ParseDirectiveZero() {
1499 CheckForValidSection();
1500
1501 int64_t NumBytes;
1502 if (ParseAbsoluteExpression(NumBytes))
1503 return true;
1504
Rafael Espindolae452b172010-10-05 19:42:57 +00001505 int64_t Val = 0;
1506 if (getLexer().is(AsmToken::Comma)) {
1507 Lex();
1508 if (ParseAbsoluteExpression(Val))
1509 return true;
1510 }
1511
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001512 if (getLexer().isNot(AsmToken::EndOfStatement))
1513 return TokError("unexpected token in '.zero' directive");
1514
1515 Lex();
1516
Rafael Espindolae452b172010-10-05 19:42:57 +00001517 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001518
1519 return false;
1520}
1521
Daniel Dunbara0d14262009-06-24 23:30:00 +00001522/// ParseDirectiveFill
1523/// ::= .fill expression , expression , expression
1524bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001525 CheckForValidSection();
1526
Daniel Dunbara0d14262009-06-24 23:30:00 +00001527 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001528 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001529 return true;
1530
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001531 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001532 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001533 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001534
Daniel Dunbara0d14262009-06-24 23:30:00 +00001535 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001536 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001537 return true;
1538
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001539 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001540 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001541 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001542
Daniel Dunbara0d14262009-06-24 23:30:00 +00001543 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001544 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001545 return true;
1546
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001547 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001548 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001549
Sean Callanan79ed1a82010-01-19 20:22:31 +00001550 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001551
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001552 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1553 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001554
1555 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001556 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001557
1558 return false;
1559}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001560
1561/// ParseDirectiveOrg
1562/// ::= .org expression [ , expression ]
1563bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001564 CheckForValidSection();
1565
Daniel Dunbar821e3332009-08-31 08:09:28 +00001566 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001567 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001568 return true;
1569
1570 // Parse optional fill expression.
1571 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001572 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1573 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001574 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001575 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001576
Daniel Dunbar475839e2009-06-29 20:37:27 +00001577 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001578 return true;
1579
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001580 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001581 return TokError("unexpected token in '.org' directive");
1582 }
1583
Sean Callanan79ed1a82010-01-19 20:22:31 +00001584 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001585
1586 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1587 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001588 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001589
1590 return false;
1591}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001592
1593/// ParseDirectiveAlign
1594/// ::= {.align, ...} expression [ , expression [ , expression ]]
1595bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001596 CheckForValidSection();
1597
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001598 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001599 int64_t Alignment;
1600 if (ParseAbsoluteExpression(Alignment))
1601 return true;
1602
1603 SMLoc MaxBytesLoc;
1604 bool HasFillExpr = false;
1605 int64_t FillExpr = 0;
1606 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001607 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1608 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001609 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001610 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001611
1612 // The fill expression can be omitted while specifying a maximum number of
1613 // alignment bytes, e.g:
1614 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001615 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001616 HasFillExpr = true;
1617 if (ParseAbsoluteExpression(FillExpr))
1618 return true;
1619 }
1620
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001621 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1622 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001623 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001624 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001625
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001626 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001627 if (ParseAbsoluteExpression(MaxBytesToFill))
1628 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001629
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001630 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001631 return TokError("unexpected token in directive");
1632 }
1633 }
1634
Sean Callanan79ed1a82010-01-19 20:22:31 +00001635 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001636
Daniel Dunbar648ac512010-05-17 21:54:30 +00001637 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001638 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001639
1640 // Compute alignment in bytes.
1641 if (IsPow2) {
1642 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001643 if (Alignment >= 32) {
1644 Error(AlignmentLoc, "invalid alignment value");
1645 Alignment = 31;
1646 }
1647
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001648 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001649 }
1650
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001651 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001652 if (MaxBytesLoc.isValid()) {
1653 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001654 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1655 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001656 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001657 }
1658
1659 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001660 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1661 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001662 MaxBytesToFill = 0;
1663 }
1664 }
1665
Daniel Dunbar648ac512010-05-17 21:54:30 +00001666 // Check whether we should use optimal code alignment for this .align
1667 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001668 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001669 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1670 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001671 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001672 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001673 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001674 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1675 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001676 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001677
1678 return false;
1679}
1680
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001681/// ParseDirectiveSymbolAttribute
1682/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001683bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001684 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001685 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001686 StringRef Name;
1687
1688 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001689 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001690
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001691 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001692
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001693 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001694
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001696 break;
1697
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001698 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001699 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001700 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001701 }
1702 }
1703
Sean Callanan79ed1a82010-01-19 20:22:31 +00001704 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001705 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001706}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001707
1708/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001709/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1710bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001711 CheckForValidSection();
1712
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001713 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001714 StringRef Name;
1715 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001716 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001717
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001718 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001719 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001720
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001721 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001722 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001723 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001724
1725 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001726 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001727 if (ParseAbsoluteExpression(Size))
1728 return true;
1729
1730 int64_t Pow2Alignment = 0;
1731 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001732 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001733 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001734 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001735 if (ParseAbsoluteExpression(Pow2Alignment))
1736 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001737
Chris Lattner258281d2010-01-19 06:22:22 +00001738 // If this target takes alignments in bytes (not log) validate and convert.
1739 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1740 if (!isPowerOf2_64(Pow2Alignment))
1741 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1742 Pow2Alignment = Log2_64(Pow2Alignment);
1743 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001744 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001745
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001746 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001747 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001748
Sean Callanan79ed1a82010-01-19 20:22:31 +00001749 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001750
Chris Lattner1fc3d752009-07-09 17:25:12 +00001751 // NOTE: a size of zero for a .comm should create a undefined symbol
1752 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001753 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001754 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1755 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001756
Eric Christopherc260a3e2010-05-14 01:38:54 +00001757 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001758 // may internally end up wanting an alignment in bytes.
1759 // FIXME: Diagnose overflow.
1760 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001761 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1762 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001763
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001764 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001765 return Error(IDLoc, "invalid symbol redefinition");
1766
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001767 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001768 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001769 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001770 getStreamer().EmitZerofill(Ctx.getMachOSection(
1771 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1772 0, SectionKind::getBSS()),
1773 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001774 return false;
1775 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001776
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001777 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001778 return false;
1779}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001780
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001781/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001782/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001783bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001784 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001785 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001786
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001787 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001788 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001789 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001790
Sean Callanan79ed1a82010-01-19 20:22:31 +00001791 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001792
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001793 if (Str.empty())
1794 Error(Loc, ".abort detected. Assembly stopping.");
1795 else
1796 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001797 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001798
1799 return false;
1800}
Kevin Enderby71148242009-07-14 21:35:03 +00001801
Kevin Enderby1f049b22009-07-14 23:21:55 +00001802/// ParseDirectiveInclude
1803/// ::= .include "filename"
1804bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001805 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001806 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001807
Sean Callanan18b83232010-01-19 21:44:56 +00001808 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001809 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001810 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001811
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001812 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001813 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001814
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001815 // Strip the quotes.
1816 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001817
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001818 // Attempt to switch the lexer to the included file before consuming the end
1819 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001820 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001821 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001822 return true;
1823 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001824
1825 return false;
1826}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001827
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001828/// ParseDirectiveIf
1829/// ::= .if expression
1830bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001831 TheCondStack.push_back(TheCondState);
1832 TheCondState.TheCond = AsmCond::IfCond;
1833 if(TheCondState.Ignore) {
1834 EatToEndOfStatement();
1835 }
1836 else {
1837 int64_t ExprValue;
1838 if (ParseAbsoluteExpression(ExprValue))
1839 return true;
1840
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001841 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001842 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001843
Sean Callanan79ed1a82010-01-19 20:22:31 +00001844 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001845
1846 TheCondState.CondMet = ExprValue;
1847 TheCondState.Ignore = !TheCondState.CondMet;
1848 }
1849
1850 return false;
1851}
1852
1853/// ParseDirectiveElseIf
1854/// ::= .elseif expression
1855bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1856 if (TheCondState.TheCond != AsmCond::IfCond &&
1857 TheCondState.TheCond != AsmCond::ElseIfCond)
1858 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1859 " an .elseif");
1860 TheCondState.TheCond = AsmCond::ElseIfCond;
1861
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001862 bool LastIgnoreState = false;
1863 if (!TheCondStack.empty())
1864 LastIgnoreState = TheCondStack.back().Ignore;
1865 if (LastIgnoreState || TheCondState.CondMet) {
1866 TheCondState.Ignore = true;
1867 EatToEndOfStatement();
1868 }
1869 else {
1870 int64_t ExprValue;
1871 if (ParseAbsoluteExpression(ExprValue))
1872 return true;
1873
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001874 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001875 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001876
Sean Callanan79ed1a82010-01-19 20:22:31 +00001877 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001878 TheCondState.CondMet = ExprValue;
1879 TheCondState.Ignore = !TheCondState.CondMet;
1880 }
1881
1882 return false;
1883}
1884
1885/// ParseDirectiveElse
1886/// ::= .else
1887bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001888 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001889 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001890
Sean Callanan79ed1a82010-01-19 20:22:31 +00001891 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001892
1893 if (TheCondState.TheCond != AsmCond::IfCond &&
1894 TheCondState.TheCond != AsmCond::ElseIfCond)
1895 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1896 ".elseif");
1897 TheCondState.TheCond = AsmCond::ElseCond;
1898 bool LastIgnoreState = false;
1899 if (!TheCondStack.empty())
1900 LastIgnoreState = TheCondStack.back().Ignore;
1901 if (LastIgnoreState || TheCondState.CondMet)
1902 TheCondState.Ignore = true;
1903 else
1904 TheCondState.Ignore = false;
1905
1906 return false;
1907}
1908
1909/// ParseDirectiveEndIf
1910/// ::= .endif
1911bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001912 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001913 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001914
Sean Callanan79ed1a82010-01-19 20:22:31 +00001915 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001916
1917 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1918 TheCondStack.empty())
1919 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1920 ".else");
1921 if (!TheCondStack.empty()) {
1922 TheCondState = TheCondStack.back();
1923 TheCondStack.pop_back();
1924 }
1925
1926 return false;
1927}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001928
1929/// ParseDirectiveFile
1930/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001931bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001932 // FIXME: I'm not sure what this is.
1933 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001934 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001935 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001936 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001937 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001938
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001939 if (FileNumber < 1)
1940 return TokError("file number less than one");
1941 }
1942
Daniel Dunbareceec052010-07-12 17:45:27 +00001943 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001944 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001945
Chris Lattnerd32e8032010-01-25 19:02:58 +00001946 StringRef Filename = getTok().getString();
1947 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001948 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001949
Daniel Dunbareceec052010-07-12 17:45:27 +00001950 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001951 return TokError("unexpected token in '.file' directive");
1952
Chris Lattnerd32e8032010-01-25 19:02:58 +00001953 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001954 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001955 else {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001956 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1957 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001958 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001959 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001960
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001961 return false;
1962}
1963
1964/// ParseDirectiveLine
1965/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001966bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001967 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1968 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001969 return TokError("unexpected token in '.line' directive");
1970
Sean Callanan18b83232010-01-19 21:44:56 +00001971 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001972 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001973 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001974
1975 // FIXME: Do something with the .line.
1976 }
1977
Daniel Dunbareceec052010-07-12 17:45:27 +00001978 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001979 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001980
1981 return false;
1982}
1983
1984
1985/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001986/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001987/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
1988/// The first number is a file number, must have been previously assigned with
1989/// a .file directive, the second number is the line number and optionally the
1990/// third number is a column position (zero if not specified). The remaining
1991/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001992bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001993
Daniel Dunbareceec052010-07-12 17:45:27 +00001994 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001995 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001996 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001997 if (FileNumber < 1)
1998 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00001999 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002000 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002001 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002002
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002003 int64_t LineNumber = 0;
2004 if (getLexer().is(AsmToken::Integer)) {
2005 LineNumber = getTok().getIntVal();
2006 if (LineNumber < 1)
2007 return TokError("line number less than one in '.loc' directive");
2008 Lex();
2009 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002010
2011 int64_t ColumnPos = 0;
2012 if (getLexer().is(AsmToken::Integer)) {
2013 ColumnPos = getTok().getIntVal();
2014 if (ColumnPos < 0)
2015 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002016 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002017 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002018
Kevin Enderbyc0957932010-09-30 16:52:03 +00002019 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002020 unsigned Isa = 0;
2021 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2022 for (;;) {
2023 if (getLexer().is(AsmToken::EndOfStatement))
2024 break;
2025
2026 StringRef Name;
2027 SMLoc Loc = getTok().getLoc();
2028 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002029 return TokError("unexpected token in '.loc' directive");
2030
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002031 if (Name == "basic_block")
2032 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2033 else if (Name == "prologue_end")
2034 Flags |= DWARF2_FLAG_PROLOGUE_END;
2035 else if (Name == "epilogue_begin")
2036 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2037 else if (Name == "is_stmt") {
2038 SMLoc Loc = getTok().getLoc();
2039 const MCExpr *Value;
2040 if (getParser().ParseExpression(Value))
2041 return true;
2042 // The expression must be the constant 0 or 1.
2043 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2044 int Value = MCE->getValue();
2045 if (Value == 0)
2046 Flags &= ~DWARF2_FLAG_IS_STMT;
2047 else if (Value == 1)
2048 Flags |= DWARF2_FLAG_IS_STMT;
2049 else
2050 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002051 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002052 else {
2053 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2054 }
2055 }
2056 else if (Name == "isa") {
2057 SMLoc Loc = getTok().getLoc();
2058 const MCExpr *Value;
2059 if (getParser().ParseExpression(Value))
2060 return true;
2061 // The expression must be a constant greater or equal to 0.
2062 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2063 int Value = MCE->getValue();
2064 if (Value < 0)
2065 return Error(Loc, "isa number less than zero");
2066 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002067 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002068 else {
2069 return Error(Loc, "isa number not a constant value");
2070 }
2071 }
2072 else {
2073 return Error(Loc, "unknown sub-directive in '.loc' directive");
2074 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002075
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002076 if (getLexer().is(AsmToken::EndOfStatement))
2077 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002078 }
2079 }
2080
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002081 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,Isa);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002082
2083 return false;
2084}
2085
Daniel Dunbar138abae2010-10-16 04:56:42 +00002086/// ParseDirectiveStabs
2087/// ::= .stabs string, number, number, number
2088bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2089 SMLoc DirectiveLoc) {
2090 return TokError("unsupported directive '" + Directive + "'");
2091}
2092
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002093/// ParseDirectiveMacrosOnOff
2094/// ::= .macros_on
2095/// ::= .macros_off
2096bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2097 SMLoc DirectiveLoc) {
2098 if (getLexer().isNot(AsmToken::EndOfStatement))
2099 return Error(getLexer().getLoc(),
2100 "unexpected token in '" + Directive + "' directive");
2101
2102 getParser().MacrosEnabled = Directive == ".macros_on";
2103
2104 return false;
2105}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002106
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002107/// ParseDirectiveMacro
2108/// ::= .macro name
2109bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2110 SMLoc DirectiveLoc) {
2111 StringRef Name;
2112 if (getParser().ParseIdentifier(Name))
2113 return TokError("expected identifier in directive");
2114
2115 if (getLexer().isNot(AsmToken::EndOfStatement))
2116 return TokError("unexpected token in '.macro' directive");
2117
2118 // Eat the end of statement.
2119 Lex();
2120
2121 AsmToken EndToken, StartToken = getTok();
2122
2123 // Lex the macro definition.
2124 for (;;) {
2125 // Check whether we have reached the end of the file.
2126 if (getLexer().is(AsmToken::Eof))
2127 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2128
2129 // Otherwise, check whether we have reach the .endmacro.
2130 if (getLexer().is(AsmToken::Identifier) &&
2131 (getTok().getIdentifier() == ".endm" ||
2132 getTok().getIdentifier() == ".endmacro")) {
2133 EndToken = getTok();
2134 Lex();
2135 if (getLexer().isNot(AsmToken::EndOfStatement))
2136 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2137 "' directive");
2138 break;
2139 }
2140
2141 // Otherwise, scan til the end of the statement.
2142 getParser().EatToEndOfStatement();
2143 }
2144
2145 if (getParser().MacroMap.lookup(Name)) {
2146 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2147 }
2148
2149 const char *BodyStart = StartToken.getLoc().getPointer();
2150 const char *BodyEnd = EndToken.getLoc().getPointer();
2151 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2152 getParser().MacroMap[Name] = new Macro(Name, Body);
2153 return false;
2154}
2155
2156/// ParseDirectiveEndMacro
2157/// ::= .endm
2158/// ::= .endmacro
2159bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2160 SMLoc DirectiveLoc) {
2161 if (getLexer().isNot(AsmToken::EndOfStatement))
2162 return TokError("unexpected token in '" + Directive + "' directive");
2163
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002164 // If we are inside a macro instantiation, terminate the current
2165 // instantiation.
2166 if (!getParser().ActiveMacros.empty()) {
2167 getParser().HandleMacroExit();
2168 return false;
2169 }
2170
2171 // Otherwise, this .endmacro is a stray entry in the file; well formed
2172 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002173 return TokError("unexpected '" + Directive + "' in file, "
2174 "no current macro definition");
2175}
2176
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002177void GenericAsmParser::ParseUleb128(uint64_t Value) {
2178 const uint64_t Mask = (1 << 7) - 1;
2179 do {
2180 unsigned Byte = Value & Mask;
2181 Value >>= 7;
2182 if (Value) // Not the last one
2183 Byte |= (1 << 7);
2184 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2185 } while (Value);
2186}
2187
2188void GenericAsmParser::ParseSleb128(int64_t Value) {
2189 const int64_t Mask = (1 << 7) - 1;
2190 for(;;) {
2191 unsigned Byte = Value & Mask;
2192 Value >>= 7;
2193 bool Done = ((Value == 0 && (Byte & 0x40) == 0) ||
2194 (Value == -1 && (Byte & 0x40) != 0));
2195 if (!Done)
2196 Byte |= (1 << 7);
2197 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2198 if (Done)
2199 break;
2200 }
2201}
2202
2203bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
2204 int64_t Value;
2205 if (getParser().ParseAbsoluteExpression(Value))
2206 return true;
2207
2208 if (getLexer().isNot(AsmToken::EndOfStatement))
2209 return TokError("unexpected token in directive");
2210
2211 if (DirName[1] == 's')
2212 ParseSleb128(Value);
2213 else
2214 ParseUleb128(Value);
2215 return false;
2216}
2217
2218
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002219/// \brief Create an MCAsmParser instance.
2220MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2221 MCContext &C, MCStreamer &Out,
2222 const MCAsmInfo &MAI) {
2223 return new AsmParser(T, SM, C, Out, MAI);
2224}