blob: c5d2aa078aab76892dde903194568f5467b1afd1 [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 Dunbar7c0a3342009-08-26 22:49:51 +000014#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000015#include "llvm/ADT/StringMap.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000020#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000021#include "llvm/MC/MCInst.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"
Bill Wendling9bc0af82009-12-28 01:34:57 +000030#include "llvm/Support/Compiler.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000031#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000032#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000033#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000034#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000036using namespace llvm;
37
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000038namespace {
39
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000040/// \brief Helper class for tracking macro definitions.
41struct Macro {
42 StringRef Name;
43 StringRef Body;
44
45public:
46 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
47};
48
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000049/// \brief Helper class for storing information about an active macro
50/// instantiation.
51struct MacroInstantiation {
52 /// The macro being instantiated.
53 const Macro *TheMacro;
54
55 /// The macro instantiation with substitutions.
56 MemoryBuffer *Instantiation;
57
58 /// The location of the instantiation.
59 SMLoc InstantiationLoc;
60
61 /// The location where parsing should resume upon instantiation completion.
62 SMLoc ExitLoc;
63
64public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000065 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
66 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000067};
68
Daniel Dunbaraef87e32010-07-18 18:31:38 +000069/// \brief The concrete assembly parser instance.
70class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000071 friend class GenericAsmParser;
72
Daniel Dunbaraef87e32010-07-18 18:31:38 +000073 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
74 void operator=(const AsmParser &); // DO NOT IMPLEMENT
75private:
76 AsmLexer Lexer;
77 MCContext &Ctx;
78 MCStreamer &Out;
79 SourceMgr &SrcMgr;
80 MCAsmParserExtension *GenericParser;
81 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000082
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083 /// This is the current buffer index we're lexing from as managed by the
84 /// SourceMgr object.
85 int CurBuffer;
86
87 AsmCond TheCondState;
88 std::vector<AsmCond> TheCondStack;
89
90 /// DirectiveMap - This is a table handlers for directives. Each handler is
91 /// invoked after the directive identifier is read and is responsible for
92 /// parsing and validating the rest of the directive. The handler is passed
93 /// in the directive name and the location of the directive keyword.
94 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000095
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000096 /// MacroMap - Map of currently defined macros.
97 StringMap<Macro*> MacroMap;
98
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000099 /// ActiveMacros - Stack of active macro instantiations.
100 std::vector<MacroInstantiation*> ActiveMacros;
101
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000102 /// Boolean tracking whether macro substitution is enabled.
103 unsigned MacrosEnabled : 1;
104
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000105 /// Flag tracking whether any errors have been encountered.
106 unsigned HadError : 1;
107
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000108public:
109 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
110 const MCAsmInfo &MAI);
111 ~AsmParser();
112
113 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
114
115 void AddDirectiveHandler(MCAsmParserExtension *Object,
116 StringRef Directive,
117 DirectiveHandler Handler) {
118 DirectiveMap[Directive] = std::make_pair(Object, Handler);
119 }
120
121public:
122 /// @name MCAsmParser Interface
123 /// {
124
125 virtual SourceMgr &getSourceManager() { return SrcMgr; }
126 virtual MCAsmLexer &getLexer() { return Lexer; }
127 virtual MCContext &getContext() { return Ctx; }
128 virtual MCStreamer &getStreamer() { return Out; }
129
130 virtual void Warning(SMLoc L, const Twine &Meg);
131 virtual bool Error(SMLoc L, const Twine &Msg);
132
133 const AsmToken &Lex();
134
135 bool ParseExpression(const MCExpr *&Res);
136 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
137 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
138 virtual bool ParseAbsoluteExpression(int64_t &Res);
139
140 /// }
141
142private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000143 void CheckForValidSection();
144
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000145 bool ParseStatement();
146
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000147 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
148 void HandleMacroExit();
149
150 void PrintMacroInstantiations();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000151 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
152
153 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
154 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000155
156 /// \brief Reset the current lexer position to that given by \arg Loc. The
157 /// current token is not set; clients should ensure Lex() is called
158 /// subsequently.
159 void JumpToLoc(SMLoc Loc);
160
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000161 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000162
163 /// \brief Parse up to the end of statement and a return the contents from the
164 /// current token until the end of the statement; the current token on exit
165 /// will be either the EndOfStatement or EOF.
166 StringRef ParseStringToEndOfStatement();
167
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000168 bool ParseAssignment(StringRef Name);
169
170 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
171 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
172 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
173
174 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
175 /// and set \arg Res to the identifier contents.
176 bool ParseIdentifier(StringRef &Res);
177
178 // Directive Parsing.
179 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
180 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
181 bool ParseDirectiveFill(); // ".fill"
182 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000183 bool ParseDirectiveZero(); // ".zero"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 bool ParseDirectiveSet(); // ".set"
185 bool ParseDirectiveOrg(); // ".org"
186 // ".align{,32}", ".p2align{,w,l}"
187 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
188
189 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
190 /// accepts a single symbol (which should be a label or an external).
191 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
192 bool ParseDirectiveELFType(); // ELF specific ".type"
193
194 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
195
196 bool ParseDirectiveAbort(); // ".abort"
197 bool ParseDirectiveInclude(); // ".include"
198
199 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
200 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
201 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
202 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
203
204 /// ParseEscapedString - Parse the current token as a string which may include
205 /// escaped characters and return the string contents.
206 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000207
208 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
209 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000210};
211
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000212/// \brief Generic implementations of directive handling, etc. which is shared
213/// (or the default, at least) for all assembler parser.
214class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000215 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
216 void AddDirectiveHandler(StringRef Directive) {
217 getParser().AddDirectiveHandler(this, Directive,
218 HandleDirective<GenericAsmParser, Handler>);
219 }
220
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000221public:
222 GenericAsmParser() {}
223
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000224 AsmParser &getParser() {
225 return (AsmParser&) this->MCAsmParserExtension::getParser();
226 }
227
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000228 virtual void Initialize(MCAsmParser &Parser) {
229 // Call the base implementation.
230 this->MCAsmParserExtension::Initialize(Parser);
231
232 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000233 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
234 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
235 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000236
237 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
239 ".macros_on");
240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
241 ".macros_off");
242 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
244 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000245
246 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000248 }
249
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000250 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
251 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
252 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000253
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000254 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000255 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
256 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000257
258 void ParseUleb128(uint64_t Value);
259 void ParseSleb128(int64_t Value);
260 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000261};
262
263}
264
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000265namespace llvm {
266
267extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000268extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000269
270}
271
Chris Lattneraaec2052010-01-19 19:46:13 +0000272enum { DEFAULT_ADDRSPACE = 0 };
273
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000274AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
275 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000276 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000277 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000278 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000279 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000280
281 // Initialize the generic parser.
282 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000283
284 // Initialize the platform / file format parser.
285 //
286 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
287 // created.
288 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000289 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000290 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000291 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000292 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000293 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000294 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000295}
296
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000297AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000298 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
299
300 // Destroy any macros.
301 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
302 ie = MacroMap.end(); it != ie; ++it)
303 delete it->getValue();
304
Daniel Dunbare4749702010-07-12 18:12:02 +0000305 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000306 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000307}
308
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000309void AsmParser::PrintMacroInstantiations() {
310 // Print the active macro instantiation stack.
311 for (std::vector<MacroInstantiation*>::const_reverse_iterator
312 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
313 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
314 "note");
315}
316
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000317void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000318 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000319 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000320}
321
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000322bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000323 HadError = true;
Sean Callananbf2013e2010-01-20 23:19:55 +0000324 PrintMessage(L, Msg.str(), "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000325 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000326 return true;
327}
328
Sean Callananbf2013e2010-01-20 23:19:55 +0000329void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
330 const char *Type) const {
331 SrcMgr.PrintMessage(Loc, Msg, Type);
332}
Sean Callananfd0b0282010-01-21 00:19:58 +0000333
334bool AsmParser::EnterIncludeFile(const std::string &Filename) {
335 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
336 if (NewBuf == -1)
337 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000338
Sean Callananfd0b0282010-01-21 00:19:58 +0000339 CurBuffer = NewBuf;
340
341 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
342
343 return false;
344}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000345
346void AsmParser::JumpToLoc(SMLoc Loc) {
347 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
348 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
349}
350
Sean Callananfd0b0282010-01-21 00:19:58 +0000351const AsmToken &AsmParser::Lex() {
352 const AsmToken *tok = &Lexer.Lex();
353
354 if (tok->is(AsmToken::Eof)) {
355 // If this is the end of an included file, pop the parent file off the
356 // include stack.
357 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
358 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000359 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000360 tok = &Lexer.Lex();
361 }
362 }
363
364 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000365 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000366
Sean Callananfd0b0282010-01-21 00:19:58 +0000367 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000368}
369
Chris Lattner79180e22010-04-05 23:15:42 +0000370bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000371 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000372 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000373 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000374
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000375 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000376 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000377
378 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000379 AsmCond StartingCondState = TheCondState;
380
Chris Lattnerb717fb02009-07-02 21:53:43 +0000381 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000382 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000383 if (!ParseStatement()) continue;
384
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000385 // We had an error, validate that one was emitted and recover by skipping to
386 // the next line.
387 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000388 EatToEndOfStatement();
389 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000390
391 if (TheCondState.TheCond != StartingCondState.TheCond ||
392 TheCondState.Ignore != StartingCondState.Ignore)
393 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000394
395 // Check to see there are no empty DwarfFile slots.
396 const std::vector<MCDwarfFile *> &MCDwarfFiles =
397 getContext().getMCDwarfFiles();
398 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000399 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000400 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000401 }
Chris Lattnerb717fb02009-07-02 21:53:43 +0000402
Chris Lattner79180e22010-04-05 23:15:42 +0000403 // Finalize the output stream if there are no errors and if the client wants
404 // us to.
405 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000406 Out.Finish();
407
Chris Lattnerb717fb02009-07-02 21:53:43 +0000408 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000409}
410
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000411void AsmParser::CheckForValidSection() {
412 if (!getStreamer().getCurrentSection()) {
413 TokError("expected section directive before assembly directive");
414 Out.SwitchSection(Ctx.getMachOSection(
415 "__TEXT", "__text",
416 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
417 0, SectionKind::getText()));
418 }
419}
420
Chris Lattner2cf5f142009-06-22 01:29:09 +0000421/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
422void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000423 while (Lexer.isNot(AsmToken::EndOfStatement) &&
424 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000425 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000426
427 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000428 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000429 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000430}
431
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000432StringRef AsmParser::ParseStringToEndOfStatement() {
433 const char *Start = getTok().getLoc().getPointer();
434
435 while (Lexer.isNot(AsmToken::EndOfStatement) &&
436 Lexer.isNot(AsmToken::Eof))
437 Lex();
438
439 const char *End = getTok().getLoc().getPointer();
440 return StringRef(Start, End - Start);
441}
Chris Lattnerc4193832009-06-22 05:51:26 +0000442
Chris Lattner74ec1a32009-06-22 06:32:03 +0000443/// ParseParenExpr - Parse a paren expression and return it.
444/// NOTE: This assumes the leading '(' has already been consumed.
445///
446/// parenexpr ::= expr)
447///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000448bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000449 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000450 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000451 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000452 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000453 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000454 return false;
455}
Chris Lattnerc4193832009-06-22 05:51:26 +0000456
Chris Lattner74ec1a32009-06-22 06:32:03 +0000457/// ParsePrimaryExpr - Parse a primary expression and return it.
458/// primaryexpr ::= (parenexpr
459/// primaryexpr ::= symbol
460/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000461/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000462/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000463bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000464 switch (Lexer.getKind()) {
465 default:
466 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000467 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000468 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000469 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000470 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000471 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000472 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000473 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000474 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000475 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000476 EndLoc = Lexer.getLoc();
477
478 StringRef Identifier;
479 if (ParseIdentifier(Identifier))
480 return false;
481
Daniel Dunbarfffff912009-10-16 01:34:54 +0000482 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000483 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000484 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000485
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000486 // Mark the symbol as used in an expression.
487 Sym->setUsedInExpr(true);
488
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000489 // Lookup the symbol variant if used.
490 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000491 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000492 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000493 if (Variant == MCSymbolRefExpr::VK_Invalid) {
494 Variant = MCSymbolRefExpr::VK_None;
495 TokError("invalid variant '" + Split.second + "'");
496 }
497 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000498
Daniel Dunbarfffff912009-10-16 01:34:54 +0000499 // If this is an absolute variable reference, substitute it now to preserve
500 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000501 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000502 if (Variant)
503 return Error(EndLoc, "unexpected modified on variable reference");
504
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000505 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000506 return false;
507 }
508
509 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000510 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000511 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000512 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000513 case AsmToken::Integer: {
514 SMLoc Loc = getTok().getLoc();
515 int64_t IntVal = getTok().getIntVal();
516 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000517 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000518 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000519 // Look for 'b' or 'f' following an Integer as a directional label
520 if (Lexer.getKind() == AsmToken::Identifier) {
521 StringRef IDVal = getTok().getString();
522 if (IDVal == "f" || IDVal == "b"){
523 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
524 IDVal == "f" ? 1 : 0);
525 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
526 getContext());
527 if(IDVal == "b" && Sym->isUndefined())
528 return Error(Loc, "invalid reference to undefined symbol");
529 EndLoc = Lexer.getLoc();
530 Lex(); // Eat identifier.
531 }
532 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000533 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000534 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000535 case AsmToken::Dot: {
536 // This is a '.' reference, which references the current PC. Emit a
537 // temporary label to the streamer and refer to it.
538 MCSymbol *Sym = Ctx.CreateTempSymbol();
539 Out.EmitLabel(Sym);
540 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
541 EndLoc = Lexer.getLoc();
542 Lex(); // Eat identifier.
543 return false;
544 }
545
Daniel Dunbar3f872332009-07-28 16:08:33 +0000546 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000547 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000548 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000549 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000550 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000551 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000552 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000553 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000554 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000555 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000556 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000557 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000558 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000559 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000560 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000561 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000562 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000563 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000564 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000565 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000566 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000567 }
568}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000569
Chris Lattnerb4307b32010-01-15 19:28:38 +0000570bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000571 SMLoc EndLoc;
572 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000573}
574
Daniel Dunbarcceba832010-09-17 02:47:07 +0000575const MCExpr *
576AsmParser::ApplyModifierToExpr(const MCExpr *E,
577 MCSymbolRefExpr::VariantKind Variant) {
578 // Recurse over the given expression, rebuilding it to apply the given variant
579 // if there is exactly one symbol.
580 switch (E->getKind()) {
581 case MCExpr::Target:
582 case MCExpr::Constant:
583 return 0;
584
585 case MCExpr::SymbolRef: {
586 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
587
588 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
589 TokError("invalid variant on expression '" +
590 getTok().getIdentifier() + "' (already modified)");
591 return E;
592 }
593
594 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
595 }
596
597 case MCExpr::Unary: {
598 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
599 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
600 if (!Sub)
601 return 0;
602 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
603 }
604
605 case MCExpr::Binary: {
606 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
607 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
608 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
609
610 if (!LHS && !RHS)
611 return 0;
612
613 if (!LHS) LHS = BE->getLHS();
614 if (!RHS) RHS = BE->getRHS();
615
616 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
617 }
618 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000619
620 assert(0 && "Invalid expression kind!");
621 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000622}
623
Chris Lattner74ec1a32009-06-22 06:32:03 +0000624/// ParseExpression - Parse an expression and return it.
625///
626/// expr ::= expr +,- expr -> lowest.
627/// expr ::= expr |,^,&,! expr -> middle.
628/// expr ::= expr *,/,%,<<,>> expr -> highest.
629/// expr ::= primaryexpr
630///
Chris Lattner54482b42010-01-15 19:39:23 +0000631bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000632 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000633 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000634 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
635 return true;
636
Daniel Dunbarcceba832010-09-17 02:47:07 +0000637 // As a special case, we support 'a op b @ modifier' by rewriting the
638 // expression to include the modifier. This is inefficient, but in general we
639 // expect users to use 'a@modifier op b'.
640 if (Lexer.getKind() == AsmToken::At) {
641 Lex();
642
643 if (Lexer.isNot(AsmToken::Identifier))
644 return TokError("unexpected symbol modifier following '@'");
645
646 MCSymbolRefExpr::VariantKind Variant =
647 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
648 if (Variant == MCSymbolRefExpr::VK_Invalid)
649 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
650
651 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
652 if (!ModifiedRes) {
653 return TokError("invalid modifier '" + getTok().getIdentifier() +
654 "' (no symbols present)");
655 return true;
656 }
657
658 Res = ModifiedRes;
659 Lex();
660 }
661
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000662 // Try to constant fold it up front, if possible.
663 int64_t Value;
664 if (Res->EvaluateAsAbsolute(Value))
665 Res = MCConstantExpr::Create(Value, getContext());
666
667 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000668}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000669
Chris Lattnerb4307b32010-01-15 19:28:38 +0000670bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000671 Res = 0;
672 return ParseParenExpr(Res, EndLoc) ||
673 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000674}
675
Daniel Dunbar475839e2009-06-29 20:37:27 +0000676bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000677 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000678
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000679 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000680 if (ParseExpression(Expr))
681 return true;
682
Daniel Dunbare00b0112009-10-16 01:57:52 +0000683 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000684 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000685
686 return false;
687}
688
Daniel Dunbar3f872332009-07-28 16:08:33 +0000689static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000690 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000691 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000692 default:
693 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000694
Daniel Dunbarcceba832010-09-17 02:47:07 +0000695 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000696 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000697 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000698 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000699 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000700 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000701 return 1;
702
703 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000704 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000705 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000706 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000707 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000708 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000709 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000710 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000711 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000712 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000713 case AsmToken::ExclaimEqual:
714 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000715 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000716 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000717 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000718 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000719 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000720 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000721 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000722 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000723 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000724 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000725 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000726 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000727 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000728 return 2;
729
730 // Intermediate Precedence: |, &, ^
731 //
732 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000733 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000734 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000735 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000736 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000737 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000738 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000739 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000740 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000741 return 3;
742
743 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000744 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000745 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000746 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000747 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000748 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000749 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000750 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000751 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000752 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000753 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000754 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000755 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000756 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000757 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000758 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000759 }
760}
761
762
763/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
764/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000765bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
766 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000767 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000768 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000769 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000770
771 // If the next token is lower precedence than we are allowed to eat, return
772 // successfully with what we ate already.
773 if (TokPrec < Precedence)
774 return false;
775
Sean Callanan79ed1a82010-01-19 20:22:31 +0000776 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000777
778 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000779 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000780 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000781
782 // If BinOp binds less tightly with RHS than the operator after RHS, let
783 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000784 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000785 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000786 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000787 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000788 }
789
Daniel Dunbar475839e2009-06-29 20:37:27 +0000790 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000791 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000792 }
793}
794
Chris Lattnerc4193832009-06-22 05:51:26 +0000795
796
797
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000798/// ParseStatement:
799/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000800/// ::= Label* Directive ...Operands... EndOfStatement
801/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000802bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000803 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000804 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000805 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000806 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000807 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000808
809 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000810 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000811 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000812 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000813 int64_t LocalLabelVal = -1;
814 // GUESS allow an integer followed by a ':' as a directional local label
815 if (Lexer.is(AsmToken::Integer)) {
816 LocalLabelVal = getTok().getIntVal();
817 if (LocalLabelVal < 0) {
818 if (!TheCondState.Ignore)
819 return TokError("unexpected token at start of statement");
820 IDVal = "";
821 }
822 else {
823 IDVal = getTok().getString();
824 Lex(); // Consume the integer token to be used as an identifier token.
825 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000826 if (!TheCondState.Ignore)
827 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000828 }
829 }
830 }
831 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000832 if (!TheCondState.Ignore)
833 return TokError("unexpected token at start of statement");
834 IDVal = "";
835 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000836
Chris Lattner7834fac2010-04-17 18:14:27 +0000837 // Handle conditional assembly here before checking for skipping. We
838 // have to do this so that .endif isn't skipped in a ".if 0" block for
839 // example.
840 if (IDVal == ".if")
841 return ParseDirectiveIf(IDLoc);
842 if (IDVal == ".elseif")
843 return ParseDirectiveElseIf(IDLoc);
844 if (IDVal == ".else")
845 return ParseDirectiveElse(IDLoc);
846 if (IDVal == ".endif")
847 return ParseDirectiveEndIf(IDLoc);
848
849 // If we are in a ".if 0" block, ignore this statement.
850 if (TheCondState.Ignore) {
851 EatToEndOfStatement();
852 return false;
853 }
854
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000855 // FIXME: Recurse on local labels?
856
857 // See what kind of statement we have.
858 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000859 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000860 CheckForValidSection();
861
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000862 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000863 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000864
865 // Diagnose attempt to use a variable as a label.
866 //
867 // FIXME: Diagnostics. Note the location of the definition as a label.
868 // FIXME: This doesn't diagnose assignment to a symbol which has been
869 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000870 MCSymbol *Sym;
871 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000872 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000873 else
874 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000875 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000876 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000877
Daniel Dunbar959fd882009-08-26 22:13:22 +0000878 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000879 Out.EmitLabel(Sym);
880
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000881 // Consume any end of statement token, if present, to avoid spurious
882 // AddBlankLine calls().
883 if (Lexer.is(AsmToken::EndOfStatement)) {
884 Lex();
885 if (Lexer.is(AsmToken::Eof))
886 return false;
887 }
888
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000889 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000890 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000891
Daniel Dunbar3f872332009-07-28 16:08:33 +0000892 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000893 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000894 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000895
Daniel Dunbare2ace502009-08-31 08:09:09 +0000896 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000897
898 default: // Normal instruction or directive.
899 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000900 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000901
902 // If macros are enabled, check to see if this is a macro instantiation.
903 if (MacrosEnabled)
904 if (const Macro *M = MacroMap.lookup(IDVal))
905 return HandleMacroEntry(IDVal, IDLoc, M);
906
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000907 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000908 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000909 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000910 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000911 return ParseDirectiveSet();
912
Daniel Dunbara0d14262009-06-24 23:30:00 +0000913 // Data directives
914
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000915 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000916 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000917 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000918 return ParseDirectiveAscii(true);
919
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000920 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000921 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000922 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000923 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000924 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000925 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000926 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000927 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000928
Eli Friedman5d68ec22010-07-19 04:17:25 +0000929 if (IDVal == ".align") {
930 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
931 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
932 }
933 if (IDVal == ".align32") {
934 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
935 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
936 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000937 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000938 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000939 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000940 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000941 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000942 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000943 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000944 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000945 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000946 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000947 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000948 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
949
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000950 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000951 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000952
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000953 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000954 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000955 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000956 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000957 if (IDVal == ".zero")
958 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000959
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000960 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000961
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000962 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000963 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000964 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000965 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000966 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000967 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000968 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000969 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000970 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000971 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000972 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000973 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000974 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000975 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000976 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000977 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000978 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000979 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000980 if (IDVal == ".type")
981 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000982 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000983 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000984 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000985 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000986 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000987 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000988 if (IDVal == ".weak_def_can_be_hidden")
989 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000990
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000991 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000992 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000993 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000994 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000995
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000996 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000997 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000998 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000999 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001000
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001001 // Look up the handler in the handler table.
1002 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1003 DirectiveMap.lookup(IDVal);
1004 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001005 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001006
Kevin Enderby9c656452009-09-10 20:51:44 +00001007 // Target hook for parsing target specific directives.
1008 if (!getTargetParser().ParseDirective(ID))
1009 return false;
1010
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001011 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001012 EatToEndOfStatement();
1013 return false;
1014 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001015
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001016 CheckForValidSection();
1017
Chris Lattnera7f13542010-05-19 23:34:33 +00001018 // Canonicalize the opcode to lower case.
1019 SmallString<128> Opcode;
1020 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1021 Opcode.push_back(tolower(IDVal[i]));
1022
Chris Lattner98986712010-01-14 22:21:20 +00001023 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001024 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001025 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001026
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001027 // Dump the parsed representation, if requested.
1028 if (getShowParsedOperands()) {
1029 SmallString<256> Str;
1030 raw_svector_ostream OS(Str);
1031 OS << "parsed instruction: [";
1032 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1033 if (i != 0)
1034 OS << ", ";
1035 ParsedOperands[i]->dump(OS);
1036 }
1037 OS << "]";
1038
1039 PrintMessage(IDLoc, OS.str(), "note");
1040 }
1041
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001042 // If parsing succeeded, match the instruction.
1043 if (!HadError) {
1044 MCInst Inst;
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001045 if (!getTargetParser().MatchInstruction(IDLoc, ParsedOperands, Inst)) {
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001046 // Emit the instruction on success.
1047 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001048 } else
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001049 HadError = true;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001050 }
Chris Lattner98986712010-01-14 22:21:20 +00001051
Chris Lattner98986712010-01-14 22:21:20 +00001052 // Free any parsed operands.
1053 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1054 delete ParsedOperands[i];
1055
Chris Lattnercbf8a982010-09-11 16:18:25 +00001056 // Don't skip the rest of the line, the instruction parser is responsible for
1057 // that.
1058 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001059}
Chris Lattner9a023f72009-06-24 04:43:34 +00001060
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001061MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1062 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001063 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1064{
1065 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1066 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001067 SmallString<256> Buf;
1068 raw_svector_ostream OS(Buf);
1069
1070 StringRef Body = M->Body;
1071 while (!Body.empty()) {
1072 // Scan for the next substitution.
1073 std::size_t End = Body.size(), Pos = 0;
1074 for (; Pos != End; ++Pos) {
1075 // Check for a substitution or escape.
1076 if (Body[Pos] != '$' || Pos + 1 == End)
1077 continue;
1078
1079 char Next = Body[Pos + 1];
1080 if (Next == '$' || Next == 'n' || isdigit(Next))
1081 break;
1082 }
1083
1084 // Add the prefix.
1085 OS << Body.slice(0, Pos);
1086
1087 // Check if we reached the end.
1088 if (Pos == End)
1089 break;
1090
1091 switch (Body[Pos+1]) {
1092 // $$ => $
1093 case '$':
1094 OS << '$';
1095 break;
1096
1097 // $n => number of arguments
1098 case 'n':
1099 OS << A.size();
1100 break;
1101
1102 // $[0-9] => argument
1103 default: {
1104 // Missing arguments are ignored.
1105 unsigned Index = Body[Pos+1] - '0';
1106 if (Index >= A.size())
1107 break;
1108
1109 // Otherwise substitute with the token values, with spaces eliminated.
1110 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1111 ie = A[Index].end(); it != ie; ++it)
1112 OS << it->getString();
1113 break;
1114 }
1115 }
1116
1117 // Update the scan point.
1118 Body = Body.substr(Pos + 2);
1119 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001120
1121 // We include the .endmacro in the buffer as our queue to exit the macro
1122 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001123 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001124
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001125 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001126}
1127
1128bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1129 const Macro *M) {
1130 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1131 // this, although we should protect against infinite loops.
1132 if (ActiveMacros.size() == 20)
1133 return TokError("macros cannot be nested more than 20 levels deep");
1134
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001135 // Parse the macro instantiation arguments.
1136 std::vector<std::vector<AsmToken> > MacroArguments;
1137 MacroArguments.push_back(std::vector<AsmToken>());
1138 unsigned ParenLevel = 0;
1139 for (;;) {
1140 if (Lexer.is(AsmToken::Eof))
1141 return TokError("unexpected token in macro instantiation");
1142 if (Lexer.is(AsmToken::EndOfStatement))
1143 break;
1144
1145 // If we aren't inside parentheses and this is a comma, start a new token
1146 // list.
1147 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1148 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001149 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001150 // Adjust the current parentheses level.
1151 if (Lexer.is(AsmToken::LParen))
1152 ++ParenLevel;
1153 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1154 --ParenLevel;
1155
1156 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001157 MacroArguments.back().push_back(getTok());
1158 }
1159 Lex();
1160 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001161
1162 // Create the macro instantiation object and add to the current macro
1163 // instantiation stack.
1164 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001165 getTok().getLoc(),
1166 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001167 ActiveMacros.push_back(MI);
1168
1169 // Jump to the macro instantiation and prime the lexer.
1170 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1171 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1172 Lex();
1173
1174 return false;
1175}
1176
1177void AsmParser::HandleMacroExit() {
1178 // Jump to the EndOfStatement we should return to, and consume it.
1179 JumpToLoc(ActiveMacros.back()->ExitLoc);
1180 Lex();
1181
1182 // Pop the instantiation entry.
1183 delete ActiveMacros.back();
1184 ActiveMacros.pop_back();
1185}
1186
Benjamin Kramer38e59892010-07-14 22:38:02 +00001187bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001188 // FIXME: Use better location, we should use proper tokens.
1189 SMLoc EqualLoc = Lexer.getLoc();
1190
Daniel Dunbar821e3332009-08-31 08:09:28 +00001191 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001192 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001193 return true;
1194
Daniel Dunbar3f872332009-07-28 16:08:33 +00001195 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001196 return TokError("unexpected token in assignment");
1197
1198 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001199 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001200
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001201 // Validate that the LHS is allowed to be a variable (either it has not been
1202 // used as a symbol, or it is an absolute symbol).
1203 MCSymbol *Sym = getContext().LookupSymbol(Name);
1204 if (Sym) {
1205 // Diagnose assignment to a label.
1206 //
1207 // FIXME: Diagnostics. Note the location of the definition as a label.
1208 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001209 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1210 ; // Allow redefinitions of undefined symbols only used in directives.
1211 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001212 return Error(EqualLoc, "redefinition of '" + Name + "'");
1213 else if (!Sym->isVariable())
1214 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001215 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001216 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1217 Name + "'");
1218 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001219 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001220
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001221 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001222
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001223 Sym->setUsedInExpr(true);
1224
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001225 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001226 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001227
1228 return false;
1229}
1230
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001231/// ParseIdentifier:
1232/// ::= identifier
1233/// ::= string
1234bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001235 // The assembler has relaxed rules for accepting identifiers, in particular we
1236 // allow things like '.globl $foo', which would normally be separate
1237 // tokens. At this level, we have already lexed so we cannot (currently)
1238 // handle this as a context dependent token, instead we detect adjacent tokens
1239 // and return the combined identifier.
1240 if (Lexer.is(AsmToken::Dollar)) {
1241 SMLoc DollarLoc = getLexer().getLoc();
1242
1243 // Consume the dollar sign, and check for a following identifier.
1244 Lex();
1245 if (Lexer.isNot(AsmToken::Identifier))
1246 return true;
1247
1248 // We have a '$' followed by an identifier, make sure they are adjacent.
1249 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1250 return true;
1251
1252 // Construct the joined identifier and consume the token.
1253 Res = StringRef(DollarLoc.getPointer(),
1254 getTok().getIdentifier().size() + 1);
1255 Lex();
1256 return false;
1257 }
1258
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001259 if (Lexer.isNot(AsmToken::Identifier) &&
1260 Lexer.isNot(AsmToken::String))
1261 return true;
1262
Sean Callanan18b83232010-01-19 21:44:56 +00001263 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001264
Sean Callanan79ed1a82010-01-19 20:22:31 +00001265 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001266
1267 return false;
1268}
1269
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001270/// ParseDirectiveSet:
1271/// ::= .set identifier ',' expression
1272bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001273 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001274
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001275 if (ParseIdentifier(Name))
1276 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001277
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001278 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001279 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001280 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001281
Daniel Dunbare2ace502009-08-31 08:09:09 +00001282 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001283}
1284
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001285bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001286 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001287
1288 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001289 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001290 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1291 if (Str[i] != '\\') {
1292 Data += Str[i];
1293 continue;
1294 }
1295
1296 // Recognize escaped characters. Note that this escape semantics currently
1297 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1298 ++i;
1299 if (i == e)
1300 return TokError("unexpected backslash at end of string");
1301
1302 // Recognize octal sequences.
1303 if ((unsigned) (Str[i] - '0') <= 7) {
1304 // Consume up to three octal characters.
1305 unsigned Value = Str[i] - '0';
1306
1307 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1308 ++i;
1309 Value = Value * 8 + (Str[i] - '0');
1310
1311 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1312 ++i;
1313 Value = Value * 8 + (Str[i] - '0');
1314 }
1315 }
1316
1317 if (Value > 255)
1318 return TokError("invalid octal escape sequence (out of range)");
1319
1320 Data += (unsigned char) Value;
1321 continue;
1322 }
1323
1324 // Otherwise recognize individual escapes.
1325 switch (Str[i]) {
1326 default:
1327 // Just reject invalid escape sequences for now.
1328 return TokError("invalid escape sequence (unrecognized character)");
1329
1330 case 'b': Data += '\b'; break;
1331 case 'f': Data += '\f'; break;
1332 case 'n': Data += '\n'; break;
1333 case 'r': Data += '\r'; break;
1334 case 't': Data += '\t'; break;
1335 case '"': Data += '"'; break;
1336 case '\\': Data += '\\'; break;
1337 }
1338 }
1339
1340 return false;
1341}
1342
Daniel Dunbara0d14262009-06-24 23:30:00 +00001343/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001344/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001345bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001346 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001347 CheckForValidSection();
1348
Daniel Dunbara0d14262009-06-24 23:30:00 +00001349 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001350 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001351 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001352
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001353 std::string Data;
1354 if (ParseEscapedString(Data))
1355 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001356
1357 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001358 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001359 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1360
Sean Callanan79ed1a82010-01-19 20:22:31 +00001361 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001362
1363 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001364 break;
1365
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001366 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001367 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001368 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001369 }
1370 }
1371
Sean Callanan79ed1a82010-01-19 20:22:31 +00001372 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001373 return false;
1374}
1375
1376/// ParseDirectiveValue
1377/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1378bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001379 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001380 CheckForValidSection();
1381
Daniel Dunbara0d14262009-06-24 23:30:00 +00001382 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001383 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001384 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001385 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001386 return true;
1387
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001388 // Special case constant expressions to match code generator.
1389 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001390 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001391 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001392 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001393
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001394 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001395 break;
1396
1397 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001398 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001399 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001400 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001401 }
1402 }
1403
Sean Callanan79ed1a82010-01-19 20:22:31 +00001404 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001405 return false;
1406}
1407
1408/// ParseDirectiveSpace
1409/// ::= .space expression [ , expression ]
1410bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001411 CheckForValidSection();
1412
Daniel Dunbara0d14262009-06-24 23:30:00 +00001413 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001414 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001415 return true;
1416
1417 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001418 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1419 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001420 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001421 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001422
Daniel Dunbar475839e2009-06-29 20:37:27 +00001423 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001424 return true;
1425
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001426 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001427 return TokError("unexpected token in '.space' directive");
1428 }
1429
Sean Callanan79ed1a82010-01-19 20:22:31 +00001430 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001431
1432 if (NumBytes <= 0)
1433 return TokError("invalid number of bytes in '.space' directive");
1434
1435 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001436 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001437
1438 return false;
1439}
1440
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001441/// ParseDirectiveZero
1442/// ::= .zero expression
1443bool AsmParser::ParseDirectiveZero() {
1444 CheckForValidSection();
1445
1446 int64_t NumBytes;
1447 if (ParseAbsoluteExpression(NumBytes))
1448 return true;
1449
1450 if (getLexer().isNot(AsmToken::EndOfStatement))
1451 return TokError("unexpected token in '.zero' directive");
1452
1453 Lex();
1454
1455 getStreamer().EmitFill(NumBytes, 0, DEFAULT_ADDRSPACE);
1456
1457 return false;
1458}
1459
Daniel Dunbara0d14262009-06-24 23:30:00 +00001460/// ParseDirectiveFill
1461/// ::= .fill expression , expression , expression
1462bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001463 CheckForValidSection();
1464
Daniel Dunbara0d14262009-06-24 23:30:00 +00001465 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001466 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001467 return true;
1468
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001469 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001470 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001471 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001472
1473 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001474 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001475 return true;
1476
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001477 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001478 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001479 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001480
1481 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001482 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001483 return true;
1484
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001485 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001486 return TokError("unexpected token in '.fill' directive");
1487
Sean Callanan79ed1a82010-01-19 20:22:31 +00001488 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001489
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001490 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1491 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001492
1493 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001494 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001495
1496 return false;
1497}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001498
1499/// ParseDirectiveOrg
1500/// ::= .org expression [ , expression ]
1501bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001502 CheckForValidSection();
1503
Daniel Dunbar821e3332009-08-31 08:09:28 +00001504 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001505 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001506 return true;
1507
1508 // Parse optional fill expression.
1509 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001510 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1511 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001512 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001513 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001514
Daniel Dunbar475839e2009-06-29 20:37:27 +00001515 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001516 return true;
1517
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001518 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001519 return TokError("unexpected token in '.org' directive");
1520 }
1521
Sean Callanan79ed1a82010-01-19 20:22:31 +00001522 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001523
1524 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1525 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001526 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001527
1528 return false;
1529}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001530
1531/// ParseDirectiveAlign
1532/// ::= {.align, ...} expression [ , expression [ , expression ]]
1533bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001534 CheckForValidSection();
1535
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001536 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001537 int64_t Alignment;
1538 if (ParseAbsoluteExpression(Alignment))
1539 return true;
1540
1541 SMLoc MaxBytesLoc;
1542 bool HasFillExpr = false;
1543 int64_t FillExpr = 0;
1544 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001545 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1546 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001547 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001548 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001549
1550 // The fill expression can be omitted while specifying a maximum number of
1551 // alignment bytes, e.g:
1552 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001553 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001554 HasFillExpr = true;
1555 if (ParseAbsoluteExpression(FillExpr))
1556 return true;
1557 }
1558
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001559 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1560 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001561 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001562 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001563
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001564 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001565 if (ParseAbsoluteExpression(MaxBytesToFill))
1566 return true;
1567
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001568 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001569 return TokError("unexpected token in directive");
1570 }
1571 }
1572
Sean Callanan79ed1a82010-01-19 20:22:31 +00001573 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001574
Daniel Dunbar648ac512010-05-17 21:54:30 +00001575 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001576 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001577
1578 // Compute alignment in bytes.
1579 if (IsPow2) {
1580 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001581 if (Alignment >= 32) {
1582 Error(AlignmentLoc, "invalid alignment value");
1583 Alignment = 31;
1584 }
1585
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001586 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001587 }
1588
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001589 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001590 if (MaxBytesLoc.isValid()) {
1591 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001592 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1593 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001594 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001595 }
1596
1597 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001598 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1599 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001600 MaxBytesToFill = 0;
1601 }
1602 }
1603
Daniel Dunbar648ac512010-05-17 21:54:30 +00001604 // Check whether we should use optimal code alignment for this .align
1605 // directive.
1606 //
1607 // FIXME: This should be using a target hook.
1608 bool UseCodeAlign = false;
1609 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001610 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001611 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001612 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1613 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001614 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001615 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001616 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001617 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1618 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001619 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001620
1621 return false;
1622}
1623
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001624/// ParseDirectiveSymbolAttribute
1625/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001626bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001627 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001628 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001629 StringRef Name;
1630
1631 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001632 return TokError("expected identifier in directive");
1633
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001634 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001635
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001636 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001637
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001638 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001639 break;
1640
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001641 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001642 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001643 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001644 }
1645 }
1646
Sean Callanan79ed1a82010-01-19 20:22:31 +00001647 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001648 return false;
1649}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001650
Matt Fleming924c5e52010-05-21 11:36:59 +00001651/// ParseDirectiveELFType
1652/// ::= .type identifier , @attribute
1653bool AsmParser::ParseDirectiveELFType() {
1654 StringRef Name;
1655 if (ParseIdentifier(Name))
1656 return TokError("expected identifier in directive");
1657
1658 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001659 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001660
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001661 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001662 return TokError("unexpected token in '.type' directive");
1663 Lex();
1664
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001665 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001666 return TokError("expected '@' before type");
1667 Lex();
1668
1669 StringRef Type;
1670 SMLoc TypeLoc;
1671
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001672 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001673 if (ParseIdentifier(Type))
1674 return TokError("expected symbol type in directive");
1675
1676 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1677 .Case("function", MCSA_ELF_TypeFunction)
1678 .Case("object", MCSA_ELF_TypeObject)
1679 .Case("tls_object", MCSA_ELF_TypeTLS)
1680 .Case("common", MCSA_ELF_TypeCommon)
1681 .Case("notype", MCSA_ELF_TypeNoType)
1682 .Default(MCSA_Invalid);
1683
1684 if (Attr == MCSA_Invalid)
1685 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1686
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001687 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001688 return TokError("unexpected token in '.type' directive");
1689
1690 Lex();
1691
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001692 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001693
1694 return false;
1695}
1696
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001697/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001698/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1699bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001700 CheckForValidSection();
1701
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001702 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001703 StringRef Name;
1704 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001705 return TokError("expected identifier in directive");
1706
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001707 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001708 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001709
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001710 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001711 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001712 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001713
1714 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001715 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001716 if (ParseAbsoluteExpression(Size))
1717 return true;
1718
1719 int64_t Pow2Alignment = 0;
1720 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001721 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001722 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001723 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001724 if (ParseAbsoluteExpression(Pow2Alignment))
1725 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001726
1727 // If this target takes alignments in bytes (not log) validate and convert.
1728 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1729 if (!isPowerOf2_64(Pow2Alignment))
1730 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1731 Pow2Alignment = Log2_64(Pow2Alignment);
1732 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001733 }
1734
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001735 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001736 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001737
Sean Callanan79ed1a82010-01-19 20:22:31 +00001738 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001739
Chris Lattner1fc3d752009-07-09 17:25:12 +00001740 // NOTE: a size of zero for a .comm should create a undefined symbol
1741 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001742 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001743 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1744 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001745
Eric Christopherc260a3e2010-05-14 01:38:54 +00001746 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001747 // may internally end up wanting an alignment in bytes.
1748 // FIXME: Diagnose overflow.
1749 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001750 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1751 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001752
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001753 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001754 return Error(IDLoc, "invalid symbol redefinition");
1755
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001756 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001757 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001758 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001759 getStreamer().EmitZerofill(Ctx.getMachOSection(
1760 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1761 0, SectionKind::getBSS()),
1762 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001763 return false;
1764 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001765
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001766 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001767 return false;
1768}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001769
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001770/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001771/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001772bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001773 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001774 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001775
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001776 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001777 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001778 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001779
Sean Callanan79ed1a82010-01-19 20:22:31 +00001780 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001781
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001782 if (Str.empty())
1783 Error(Loc, ".abort detected. Assembly stopping.");
1784 else
1785 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001786 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001787
1788 return false;
1789}
Kevin Enderby71148242009-07-14 21:35:03 +00001790
Kevin Enderby1f049b22009-07-14 23:21:55 +00001791/// ParseDirectiveInclude
1792/// ::= .include "filename"
1793bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001794 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001795 return TokError("expected string in '.include' directive");
1796
Sean Callanan18b83232010-01-19 21:44:56 +00001797 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001798 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001799 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001800
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001801 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001802 return TokError("unexpected token in '.include' directive");
1803
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001804 // Strip the quotes.
1805 Filename = Filename.substr(1, Filename.size()-2);
1806
1807 // Attempt to switch the lexer to the included file before consuming the end
1808 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001809 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001810 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001811 return true;
1812 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001813
1814 return false;
1815}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001816
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001817/// ParseDirectiveIf
1818/// ::= .if expression
1819bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001820 TheCondStack.push_back(TheCondState);
1821 TheCondState.TheCond = AsmCond::IfCond;
1822 if(TheCondState.Ignore) {
1823 EatToEndOfStatement();
1824 }
1825 else {
1826 int64_t ExprValue;
1827 if (ParseAbsoluteExpression(ExprValue))
1828 return true;
1829
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001830 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001831 return TokError("unexpected token in '.if' directive");
1832
Sean Callanan79ed1a82010-01-19 20:22:31 +00001833 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001834
1835 TheCondState.CondMet = ExprValue;
1836 TheCondState.Ignore = !TheCondState.CondMet;
1837 }
1838
1839 return false;
1840}
1841
1842/// ParseDirectiveElseIf
1843/// ::= .elseif expression
1844bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1845 if (TheCondState.TheCond != AsmCond::IfCond &&
1846 TheCondState.TheCond != AsmCond::ElseIfCond)
1847 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1848 " an .elseif");
1849 TheCondState.TheCond = AsmCond::ElseIfCond;
1850
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001851 bool LastIgnoreState = false;
1852 if (!TheCondStack.empty())
1853 LastIgnoreState = TheCondStack.back().Ignore;
1854 if (LastIgnoreState || TheCondState.CondMet) {
1855 TheCondState.Ignore = true;
1856 EatToEndOfStatement();
1857 }
1858 else {
1859 int64_t ExprValue;
1860 if (ParseAbsoluteExpression(ExprValue))
1861 return true;
1862
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001863 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001864 return TokError("unexpected token in '.elseif' directive");
1865
Sean Callanan79ed1a82010-01-19 20:22:31 +00001866 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001867 TheCondState.CondMet = ExprValue;
1868 TheCondState.Ignore = !TheCondState.CondMet;
1869 }
1870
1871 return false;
1872}
1873
1874/// ParseDirectiveElse
1875/// ::= .else
1876bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001877 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001878 return TokError("unexpected token in '.else' directive");
1879
Sean Callanan79ed1a82010-01-19 20:22:31 +00001880 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001881
1882 if (TheCondState.TheCond != AsmCond::IfCond &&
1883 TheCondState.TheCond != AsmCond::ElseIfCond)
1884 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1885 ".elseif");
1886 TheCondState.TheCond = AsmCond::ElseCond;
1887 bool LastIgnoreState = false;
1888 if (!TheCondStack.empty())
1889 LastIgnoreState = TheCondStack.back().Ignore;
1890 if (LastIgnoreState || TheCondState.CondMet)
1891 TheCondState.Ignore = true;
1892 else
1893 TheCondState.Ignore = false;
1894
1895 return false;
1896}
1897
1898/// ParseDirectiveEndIf
1899/// ::= .endif
1900bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001901 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001902 return TokError("unexpected token in '.endif' directive");
1903
Sean Callanan79ed1a82010-01-19 20:22:31 +00001904 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001905
1906 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1907 TheCondStack.empty())
1908 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1909 ".else");
1910 if (!TheCondStack.empty()) {
1911 TheCondState = TheCondStack.back();
1912 TheCondStack.pop_back();
1913 }
1914
1915 return false;
1916}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001917
1918/// ParseDirectiveFile
1919/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001920bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001921 // FIXME: I'm not sure what this is.
1922 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001923 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001924 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001925 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001926 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001927
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001928 if (FileNumber < 1)
1929 return TokError("file number less than one");
1930 }
1931
Daniel Dunbareceec052010-07-12 17:45:27 +00001932 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001933 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001934
Chris Lattnerd32e8032010-01-25 19:02:58 +00001935 StringRef Filename = getTok().getString();
1936 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001937 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001938
Daniel Dunbareceec052010-07-12 17:45:27 +00001939 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001940 return TokError("unexpected token in '.file' directive");
1941
Chris Lattnerd32e8032010-01-25 19:02:58 +00001942 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001943 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001944 else {
1945 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1946 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001947 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001948 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001949
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001950 return false;
1951}
1952
1953/// ParseDirectiveLine
1954/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001955bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001956 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1957 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001958 return TokError("unexpected token in '.line' directive");
1959
Sean Callanan18b83232010-01-19 21:44:56 +00001960 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001961 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001962 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001963
1964 // FIXME: Do something with the .line.
1965 }
1966
Daniel Dunbareceec052010-07-12 17:45:27 +00001967 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001968 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001969
1970 return false;
1971}
1972
1973
1974/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001975/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001976/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
1977/// The first number is a file number, must have been previously assigned with
1978/// a .file directive, the second number is the line number and optionally the
1979/// third number is a column position (zero if not specified). The remaining
1980/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001981bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001982
Daniel Dunbareceec052010-07-12 17:45:27 +00001983 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001984 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001985 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001986 if (FileNumber < 1)
1987 return TokError("file number less than one in '.loc' directive");
1988 if (!getContext().ValidateDwarfFileNumber(FileNumber))
1989 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001990 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001991
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001992 int64_t LineNumber = 0;
1993 if (getLexer().is(AsmToken::Integer)) {
1994 LineNumber = getTok().getIntVal();
1995 if (LineNumber < 1)
1996 return TokError("line number less than one in '.loc' directive");
1997 Lex();
1998 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001999
2000 int64_t ColumnPos = 0;
2001 if (getLexer().is(AsmToken::Integer)) {
2002 ColumnPos = getTok().getIntVal();
2003 if (ColumnPos < 0)
2004 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002005 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002006 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002007
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002008 unsigned Flags = 0;
2009 unsigned Isa = 0;
2010 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2011 for (;;) {
2012 if (getLexer().is(AsmToken::EndOfStatement))
2013 break;
2014
2015 StringRef Name;
2016 SMLoc Loc = getTok().getLoc();
2017 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002018 return TokError("unexpected token in '.loc' directive");
2019
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002020 if (Name == "basic_block")
2021 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2022 else if (Name == "prologue_end")
2023 Flags |= DWARF2_FLAG_PROLOGUE_END;
2024 else if (Name == "epilogue_begin")
2025 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2026 else if (Name == "is_stmt") {
2027 SMLoc Loc = getTok().getLoc();
2028 const MCExpr *Value;
2029 if (getParser().ParseExpression(Value))
2030 return true;
2031 // The expression must be the constant 0 or 1.
2032 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2033 int Value = MCE->getValue();
2034 if (Value == 0)
2035 Flags &= ~DWARF2_FLAG_IS_STMT;
2036 else if (Value == 1)
2037 Flags |= DWARF2_FLAG_IS_STMT;
2038 else
2039 return Error(Loc, "is_stmt value not 0 or 1");
2040 }
2041 else {
2042 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2043 }
2044 }
2045 else if (Name == "isa") {
2046 SMLoc Loc = getTok().getLoc();
2047 const MCExpr *Value;
2048 if (getParser().ParseExpression(Value))
2049 return true;
2050 // The expression must be a constant greater or equal to 0.
2051 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2052 int Value = MCE->getValue();
2053 if (Value < 0)
2054 return Error(Loc, "isa number less than zero");
2055 Isa = Value;
2056 }
2057 else {
2058 return Error(Loc, "isa number not a constant value");
2059 }
2060 }
2061 else {
2062 return Error(Loc, "unknown sub-directive in '.loc' directive");
2063 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002064
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002065 if (getLexer().is(AsmToken::EndOfStatement))
2066 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002067 }
2068 }
2069
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002070 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,Isa);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002071
2072 return false;
2073}
2074
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002075/// ParseDirectiveMacrosOnOff
2076/// ::= .macros_on
2077/// ::= .macros_off
2078bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2079 SMLoc DirectiveLoc) {
2080 if (getLexer().isNot(AsmToken::EndOfStatement))
2081 return Error(getLexer().getLoc(),
2082 "unexpected token in '" + Directive + "' directive");
2083
2084 getParser().MacrosEnabled = Directive == ".macros_on";
2085
2086 return false;
2087}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002088
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002089/// ParseDirectiveMacro
2090/// ::= .macro name
2091bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2092 SMLoc DirectiveLoc) {
2093 StringRef Name;
2094 if (getParser().ParseIdentifier(Name))
2095 return TokError("expected identifier in directive");
2096
2097 if (getLexer().isNot(AsmToken::EndOfStatement))
2098 return TokError("unexpected token in '.macro' directive");
2099
2100 // Eat the end of statement.
2101 Lex();
2102
2103 AsmToken EndToken, StartToken = getTok();
2104
2105 // Lex the macro definition.
2106 for (;;) {
2107 // Check whether we have reached the end of the file.
2108 if (getLexer().is(AsmToken::Eof))
2109 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2110
2111 // Otherwise, check whether we have reach the .endmacro.
2112 if (getLexer().is(AsmToken::Identifier) &&
2113 (getTok().getIdentifier() == ".endm" ||
2114 getTok().getIdentifier() == ".endmacro")) {
2115 EndToken = getTok();
2116 Lex();
2117 if (getLexer().isNot(AsmToken::EndOfStatement))
2118 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2119 "' directive");
2120 break;
2121 }
2122
2123 // Otherwise, scan til the end of the statement.
2124 getParser().EatToEndOfStatement();
2125 }
2126
2127 if (getParser().MacroMap.lookup(Name)) {
2128 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2129 }
2130
2131 const char *BodyStart = StartToken.getLoc().getPointer();
2132 const char *BodyEnd = EndToken.getLoc().getPointer();
2133 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2134 getParser().MacroMap[Name] = new Macro(Name, Body);
2135 return false;
2136}
2137
2138/// ParseDirectiveEndMacro
2139/// ::= .endm
2140/// ::= .endmacro
2141bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2142 SMLoc DirectiveLoc) {
2143 if (getLexer().isNot(AsmToken::EndOfStatement))
2144 return TokError("unexpected token in '" + Directive + "' directive");
2145
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002146 // If we are inside a macro instantiation, terminate the current
2147 // instantiation.
2148 if (!getParser().ActiveMacros.empty()) {
2149 getParser().HandleMacroExit();
2150 return false;
2151 }
2152
2153 // Otherwise, this .endmacro is a stray entry in the file; well formed
2154 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002155 return TokError("unexpected '" + Directive + "' in file, "
2156 "no current macro definition");
2157}
2158
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002159void GenericAsmParser::ParseUleb128(uint64_t Value) {
2160 const uint64_t Mask = (1 << 7) - 1;
2161 do {
2162 unsigned Byte = Value & Mask;
2163 Value >>= 7;
2164 if (Value) // Not the last one
2165 Byte |= (1 << 7);
2166 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2167 } while (Value);
2168}
2169
2170void GenericAsmParser::ParseSleb128(int64_t Value) {
2171 const int64_t Mask = (1 << 7) - 1;
2172 for(;;) {
2173 unsigned Byte = Value & Mask;
2174 Value >>= 7;
2175 bool Done = ((Value == 0 && (Byte & 0x40) == 0) ||
2176 (Value == -1 && (Byte & 0x40) != 0));
2177 if (!Done)
2178 Byte |= (1 << 7);
2179 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2180 if (Done)
2181 break;
2182 }
2183}
2184
2185bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
2186 int64_t Value;
2187 if (getParser().ParseAbsoluteExpression(Value))
2188 return true;
2189
2190 if (getLexer().isNot(AsmToken::EndOfStatement))
2191 return TokError("unexpected token in directive");
2192
2193 if (DirName[1] == 's')
2194 ParseSleb128(Value);
2195 else
2196 ParseUleb128(Value);
2197 return false;
2198}
2199
2200
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002201/// \brief Create an MCAsmParser instance.
2202MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2203 MCContext &C, MCStreamer &Out,
2204 const MCAsmInfo &MAI) {
2205 return new AsmParser(T, SM, C, Out, MAI);
2206}