blob: b3ffed846d5d0a4270993ed1ceb1f9833fa47ca1 [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"
183 bool ParseDirectiveSet(); // ".set"
184 bool ParseDirectiveOrg(); // ".org"
185 // ".align{,32}", ".p2align{,w,l}"
186 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
187
188 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
189 /// accepts a single symbol (which should be a label or an external).
190 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
191 bool ParseDirectiveELFType(); // ELF specific ".type"
192
193 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
194
195 bool ParseDirectiveAbort(); // ".abort"
196 bool ParseDirectiveInclude(); // ".include"
197
198 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
199 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
200 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
201 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
202
203 /// ParseEscapedString - Parse the current token as a string which may include
204 /// escaped characters and return the string contents.
205 bool ParseEscapedString(std::string &Data);
206};
207
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000208/// \brief Generic implementations of directive handling, etc. which is shared
209/// (or the default, at least) for all assembler parser.
210class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000211 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
212 void AddDirectiveHandler(StringRef Directive) {
213 getParser().AddDirectiveHandler(this, Directive,
214 HandleDirective<GenericAsmParser, Handler>);
215 }
216
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000217public:
218 GenericAsmParser() {}
219
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000220 AsmParser &getParser() {
221 return (AsmParser&) this->MCAsmParserExtension::getParser();
222 }
223
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000224 virtual void Initialize(MCAsmParser &Parser) {
225 // Call the base implementation.
226 this->MCAsmParserExtension::Initialize(Parser);
227
228 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000229 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
230 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
231 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000232
233 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000234 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
235 ".macros_on");
236 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
237 ".macros_off");
238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000241
242 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000244 }
245
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000246 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
247 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
248 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000249
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000250 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000251 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
252 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000253
254 void ParseUleb128(uint64_t Value);
255 void ParseSleb128(int64_t Value);
256 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000257};
258
259}
260
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000261namespace llvm {
262
263extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000264extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000265
266}
267
Chris Lattneraaec2052010-01-19 19:46:13 +0000268enum { DEFAULT_ADDRSPACE = 0 };
269
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000270AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
271 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000272 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000273 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000274 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000275 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000276
277 // Initialize the generic parser.
278 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000279
280 // Initialize the platform / file format parser.
281 //
282 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
283 // created.
284 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000285 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000286 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000287 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000288 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000289 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000290 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000291}
292
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000293AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000294 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
295
296 // Destroy any macros.
297 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
298 ie = MacroMap.end(); it != ie; ++it)
299 delete it->getValue();
300
Daniel Dunbare4749702010-07-12 18:12:02 +0000301 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000302 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000303}
304
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000305void AsmParser::PrintMacroInstantiations() {
306 // Print the active macro instantiation stack.
307 for (std::vector<MacroInstantiation*>::const_reverse_iterator
308 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
309 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
310 "note");
311}
312
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000313void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000314 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000315 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000316}
317
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000318bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000319 HadError = true;
Sean Callananbf2013e2010-01-20 23:19:55 +0000320 PrintMessage(L, Msg.str(), "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000321 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000322 return true;
323}
324
Sean Callananbf2013e2010-01-20 23:19:55 +0000325void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
326 const char *Type) const {
327 SrcMgr.PrintMessage(Loc, Msg, Type);
328}
Sean Callananfd0b0282010-01-21 00:19:58 +0000329
330bool AsmParser::EnterIncludeFile(const std::string &Filename) {
331 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
332 if (NewBuf == -1)
333 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000334
Sean Callananfd0b0282010-01-21 00:19:58 +0000335 CurBuffer = NewBuf;
336
337 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
338
339 return false;
340}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000341
342void AsmParser::JumpToLoc(SMLoc Loc) {
343 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
344 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
345}
346
Sean Callananfd0b0282010-01-21 00:19:58 +0000347const AsmToken &AsmParser::Lex() {
348 const AsmToken *tok = &Lexer.Lex();
349
350 if (tok->is(AsmToken::Eof)) {
351 // If this is the end of an included file, pop the parent file off the
352 // include stack.
353 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
354 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000355 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000356 tok = &Lexer.Lex();
357 }
358 }
359
360 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000361 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000362
Sean Callananfd0b0282010-01-21 00:19:58 +0000363 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000364}
365
Chris Lattner79180e22010-04-05 23:15:42 +0000366bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000367 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000368 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000369 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000370
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000371 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000372 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000373
374 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000375 AsmCond StartingCondState = TheCondState;
376
Chris Lattnerb717fb02009-07-02 21:53:43 +0000377 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000378 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000379 if (!ParseStatement()) continue;
380
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000381 // We had an error, validate that one was emitted and recover by skipping to
382 // the next line.
383 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000384 EatToEndOfStatement();
385 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000386
387 if (TheCondState.TheCond != StartingCondState.TheCond ||
388 TheCondState.Ignore != StartingCondState.Ignore)
389 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000390
391 // Check to see there are no empty DwarfFile slots.
392 const std::vector<MCDwarfFile *> &MCDwarfFiles =
393 getContext().getMCDwarfFiles();
394 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000395 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000396 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000397 }
Chris Lattnerb717fb02009-07-02 21:53:43 +0000398
Chris Lattner79180e22010-04-05 23:15:42 +0000399 // Finalize the output stream if there are no errors and if the client wants
400 // us to.
401 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000402 Out.Finish();
403
Chris Lattnerb717fb02009-07-02 21:53:43 +0000404 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000405}
406
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000407void AsmParser::CheckForValidSection() {
408 if (!getStreamer().getCurrentSection()) {
409 TokError("expected section directive before assembly directive");
410 Out.SwitchSection(Ctx.getMachOSection(
411 "__TEXT", "__text",
412 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
413 0, SectionKind::getText()));
414 }
415}
416
Chris Lattner2cf5f142009-06-22 01:29:09 +0000417/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
418void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000419 while (Lexer.isNot(AsmToken::EndOfStatement) &&
420 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000421 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000422
423 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000424 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000425 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000426}
427
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000428StringRef AsmParser::ParseStringToEndOfStatement() {
429 const char *Start = getTok().getLoc().getPointer();
430
431 while (Lexer.isNot(AsmToken::EndOfStatement) &&
432 Lexer.isNot(AsmToken::Eof))
433 Lex();
434
435 const char *End = getTok().getLoc().getPointer();
436 return StringRef(Start, End - Start);
437}
Chris Lattnerc4193832009-06-22 05:51:26 +0000438
Chris Lattner74ec1a32009-06-22 06:32:03 +0000439/// ParseParenExpr - Parse a paren expression and return it.
440/// NOTE: This assumes the leading '(' has already been consumed.
441///
442/// parenexpr ::= expr)
443///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000444bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000445 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000446 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000447 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000448 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000449 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000450 return false;
451}
Chris Lattnerc4193832009-06-22 05:51:26 +0000452
Chris Lattner74ec1a32009-06-22 06:32:03 +0000453/// ParsePrimaryExpr - Parse a primary expression and return it.
454/// primaryexpr ::= (parenexpr
455/// primaryexpr ::= symbol
456/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000457/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000458/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000459bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000460 switch (Lexer.getKind()) {
461 default:
462 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000463 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000464 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000465 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000466 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000467 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000468 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000469 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000470 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000471 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000472 EndLoc = Lexer.getLoc();
473
474 StringRef Identifier;
475 if (ParseIdentifier(Identifier))
476 return false;
477
Daniel Dunbarfffff912009-10-16 01:34:54 +0000478 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000479 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000480 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000481
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000482 // Mark the symbol as used in an expression.
483 Sym->setUsedInExpr(true);
484
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000485 // Lookup the symbol variant if used.
486 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000487 if (Split.first.size() != Identifier.size())
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000488 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
489
Daniel Dunbarfffff912009-10-16 01:34:54 +0000490 // If this is an absolute variable reference, substitute it now to preserve
491 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000492 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000493 if (Variant)
494 return Error(EndLoc, "unexpected modified on variable reference");
495
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000496 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000497 return false;
498 }
499
500 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000501 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000502 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000503 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000504 case AsmToken::Integer: {
505 SMLoc Loc = getTok().getLoc();
506 int64_t IntVal = getTok().getIntVal();
507 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000508 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000509 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000510 // Look for 'b' or 'f' following an Integer as a directional label
511 if (Lexer.getKind() == AsmToken::Identifier) {
512 StringRef IDVal = getTok().getString();
513 if (IDVal == "f" || IDVal == "b"){
514 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
515 IDVal == "f" ? 1 : 0);
516 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
517 getContext());
518 if(IDVal == "b" && Sym->isUndefined())
519 return Error(Loc, "invalid reference to undefined symbol");
520 EndLoc = Lexer.getLoc();
521 Lex(); // Eat identifier.
522 }
523 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000524 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000525 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000526 case AsmToken::Dot: {
527 // This is a '.' reference, which references the current PC. Emit a
528 // temporary label to the streamer and refer to it.
529 MCSymbol *Sym = Ctx.CreateTempSymbol();
530 Out.EmitLabel(Sym);
531 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
532 EndLoc = Lexer.getLoc();
533 Lex(); // Eat identifier.
534 return false;
535 }
536
Daniel Dunbar3f872332009-07-28 16:08:33 +0000537 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000538 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000539 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000540 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000541 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000542 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000543 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000544 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000545 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000546 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000547 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000548 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000549 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000550 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000551 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000552 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000553 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000554 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000555 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000556 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000557 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000558 }
559}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000560
Chris Lattnerb4307b32010-01-15 19:28:38 +0000561bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000562 SMLoc EndLoc;
563 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000564}
565
Chris Lattner74ec1a32009-06-22 06:32:03 +0000566/// ParseExpression - Parse an expression and return it.
567///
568/// expr ::= expr +,- expr -> lowest.
569/// expr ::= expr |,^,&,! expr -> middle.
570/// expr ::= expr *,/,%,<<,>> expr -> highest.
571/// expr ::= primaryexpr
572///
Chris Lattner54482b42010-01-15 19:39:23 +0000573bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000574 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000575 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000576 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
577 return true;
578
579 // Try to constant fold it up front, if possible.
580 int64_t Value;
581 if (Res->EvaluateAsAbsolute(Value))
582 Res = MCConstantExpr::Create(Value, getContext());
583
584 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000585}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000586
Chris Lattnerb4307b32010-01-15 19:28:38 +0000587bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000588 Res = 0;
589 return ParseParenExpr(Res, EndLoc) ||
590 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000591}
592
Daniel Dunbar475839e2009-06-29 20:37:27 +0000593bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000594 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000595
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000596 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000597 if (ParseExpression(Expr))
598 return true;
599
Daniel Dunbare00b0112009-10-16 01:57:52 +0000600 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000601 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000602
603 return false;
604}
605
Daniel Dunbar3f872332009-07-28 16:08:33 +0000606static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000607 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000608 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000609 default:
610 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000611
612 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000613 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000614 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000615 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000616 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000617 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000618 return 1;
619
620 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000621 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000622 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000623 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000624 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000625 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000626 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000627 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000628 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000629 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000630 case AsmToken::ExclaimEqual:
631 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000632 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000633 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000634 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000635 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000636 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000637 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000638 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000639 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000640 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000641 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000642 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000643 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000644 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000645 return 2;
646
647 // Intermediate Precedence: |, &, ^
648 //
649 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000650 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000651 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000652 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000653 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000654 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000655 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000656 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000657 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000658 return 3;
659
660 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000661 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000662 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000663 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000664 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000665 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000666 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000667 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000668 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000669 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000670 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000671 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000672 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000673 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000674 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000675 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000676 }
677}
678
679
680/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
681/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000682bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
683 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000684 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000685 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000686 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000687
688 // If the next token is lower precedence than we are allowed to eat, return
689 // successfully with what we ate already.
690 if (TokPrec < Precedence)
691 return false;
692
Sean Callanan79ed1a82010-01-19 20:22:31 +0000693 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000694
695 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000696 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000697 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000698
699 // If BinOp binds less tightly with RHS than the operator after RHS, let
700 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000701 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000702 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000703 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000704 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000705 }
706
Daniel Dunbar475839e2009-06-29 20:37:27 +0000707 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000708 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000709 }
710}
711
Chris Lattnerc4193832009-06-22 05:51:26 +0000712
713
714
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000715/// ParseStatement:
716/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000717/// ::= Label* Directive ...Operands... EndOfStatement
718/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000719bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000720 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000721 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000722 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000723 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000724 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000725
726 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000727 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000728 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000729 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000730 int64_t LocalLabelVal = -1;
731 // GUESS allow an integer followed by a ':' as a directional local label
732 if (Lexer.is(AsmToken::Integer)) {
733 LocalLabelVal = getTok().getIntVal();
734 if (LocalLabelVal < 0) {
735 if (!TheCondState.Ignore)
736 return TokError("unexpected token at start of statement");
737 IDVal = "";
738 }
739 else {
740 IDVal = getTok().getString();
741 Lex(); // Consume the integer token to be used as an identifier token.
742 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000743 if (!TheCondState.Ignore)
744 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000745 }
746 }
747 }
748 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000749 if (!TheCondState.Ignore)
750 return TokError("unexpected token at start of statement");
751 IDVal = "";
752 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000753
Chris Lattner7834fac2010-04-17 18:14:27 +0000754 // Handle conditional assembly here before checking for skipping. We
755 // have to do this so that .endif isn't skipped in a ".if 0" block for
756 // example.
757 if (IDVal == ".if")
758 return ParseDirectiveIf(IDLoc);
759 if (IDVal == ".elseif")
760 return ParseDirectiveElseIf(IDLoc);
761 if (IDVal == ".else")
762 return ParseDirectiveElse(IDLoc);
763 if (IDVal == ".endif")
764 return ParseDirectiveEndIf(IDLoc);
765
766 // If we are in a ".if 0" block, ignore this statement.
767 if (TheCondState.Ignore) {
768 EatToEndOfStatement();
769 return false;
770 }
771
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000772 // FIXME: Recurse on local labels?
773
774 // See what kind of statement we have.
775 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000776 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000777 CheckForValidSection();
778
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000779 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000780 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000781
782 // Diagnose attempt to use a variable as a label.
783 //
784 // FIXME: Diagnostics. Note the location of the definition as a label.
785 // FIXME: This doesn't diagnose assignment to a symbol which has been
786 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000787 MCSymbol *Sym;
788 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000789 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000790 else
791 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000792 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000793 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000794
Daniel Dunbar959fd882009-08-26 22:13:22 +0000795 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000796 Out.EmitLabel(Sym);
797
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000798 // Consume any end of statement token, if present, to avoid spurious
799 // AddBlankLine calls().
800 if (Lexer.is(AsmToken::EndOfStatement)) {
801 Lex();
802 if (Lexer.is(AsmToken::Eof))
803 return false;
804 }
805
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000806 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000807 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000808
Daniel Dunbar3f872332009-07-28 16:08:33 +0000809 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000810 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000811 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000812
Daniel Dunbare2ace502009-08-31 08:09:09 +0000813 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000814
815 default: // Normal instruction or directive.
816 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000817 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000818
819 // If macros are enabled, check to see if this is a macro instantiation.
820 if (MacrosEnabled)
821 if (const Macro *M = MacroMap.lookup(IDVal))
822 return HandleMacroEntry(IDVal, IDLoc, M);
823
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000824 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000825 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000826 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000827 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000828 return ParseDirectiveSet();
829
Daniel Dunbara0d14262009-06-24 23:30:00 +0000830 // Data directives
831
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000832 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000833 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000834 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000835 return ParseDirectiveAscii(true);
836
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000837 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000838 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000839 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000840 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000841 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000842 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000843 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000844 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000845
Eli Friedman5d68ec22010-07-19 04:17:25 +0000846 if (IDVal == ".align") {
847 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
848 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
849 }
850 if (IDVal == ".align32") {
851 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
852 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
853 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000854 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000855 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000856 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000857 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000858 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000859 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000860 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000861 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000862 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000863 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000864 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000865 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
866
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000867 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000868 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000869
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000870 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000871 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000872 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000873 return ParseDirectiveSpace();
874
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000875 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000876
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000877 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000878 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000879 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000880 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000881 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000882 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000883 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000884 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000885 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000886 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000887 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000888 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000889 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000890 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000891 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000892 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000893 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000894 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000895 if (IDVal == ".type")
896 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000897 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000898 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000899 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000900 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000901 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000902 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000903 if (IDVal == ".weak_def_can_be_hidden")
904 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000905
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000906 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000907 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000908 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000909 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000910
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000911 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000912 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000913 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000914 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000915
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000916 // Look up the handler in the handler table.
917 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
918 DirectiveMap.lookup(IDVal);
919 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000920 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000921
Kevin Enderby9c656452009-09-10 20:51:44 +0000922 // Target hook for parsing target specific directives.
923 if (!getTargetParser().ParseDirective(ID))
924 return false;
925
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000926 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000927 EatToEndOfStatement();
928 return false;
929 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000930
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000931 CheckForValidSection();
932
Chris Lattnera7f13542010-05-19 23:34:33 +0000933 // Canonicalize the opcode to lower case.
934 SmallString<128> Opcode;
935 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
936 Opcode.push_back(tolower(IDVal[i]));
937
Chris Lattner98986712010-01-14 22:21:20 +0000938 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000939 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000940 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000941
Daniel Dunbar3c14ca42010-08-11 06:37:09 +0000942 // Dump the parsed representation, if requested.
943 if (getShowParsedOperands()) {
944 SmallString<256> Str;
945 raw_svector_ostream OS(Str);
946 OS << "parsed instruction: [";
947 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
948 if (i != 0)
949 OS << ", ";
950 ParsedOperands[i]->dump(OS);
951 }
952 OS << "]";
953
954 PrintMessage(IDLoc, OS.str(), "note");
955 }
956
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000957 // If parsing succeeded, match the instruction.
958 if (!HadError) {
959 MCInst Inst;
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000960 if (!getTargetParser().MatchInstruction(IDLoc, ParsedOperands, Inst)) {
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000961 // Emit the instruction on success.
962 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000963 } else
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000964 HadError = true;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000965 }
Chris Lattner98986712010-01-14 22:21:20 +0000966
Chris Lattner98986712010-01-14 22:21:20 +0000967 // Free any parsed operands.
968 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
969 delete ParsedOperands[i];
970
Chris Lattnercbf8a982010-09-11 16:18:25 +0000971 // Don't skip the rest of the line, the instruction parser is responsible for
972 // that.
973 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000974}
Chris Lattner9a023f72009-06-24 04:43:34 +0000975
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000976MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
977 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000978 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
979{
980 // Macro instantiation is lexical, unfortunately. We construct a new buffer
981 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000982 SmallString<256> Buf;
983 raw_svector_ostream OS(Buf);
984
985 StringRef Body = M->Body;
986 while (!Body.empty()) {
987 // Scan for the next substitution.
988 std::size_t End = Body.size(), Pos = 0;
989 for (; Pos != End; ++Pos) {
990 // Check for a substitution or escape.
991 if (Body[Pos] != '$' || Pos + 1 == End)
992 continue;
993
994 char Next = Body[Pos + 1];
995 if (Next == '$' || Next == 'n' || isdigit(Next))
996 break;
997 }
998
999 // Add the prefix.
1000 OS << Body.slice(0, Pos);
1001
1002 // Check if we reached the end.
1003 if (Pos == End)
1004 break;
1005
1006 switch (Body[Pos+1]) {
1007 // $$ => $
1008 case '$':
1009 OS << '$';
1010 break;
1011
1012 // $n => number of arguments
1013 case 'n':
1014 OS << A.size();
1015 break;
1016
1017 // $[0-9] => argument
1018 default: {
1019 // Missing arguments are ignored.
1020 unsigned Index = Body[Pos+1] - '0';
1021 if (Index >= A.size())
1022 break;
1023
1024 // Otherwise substitute with the token values, with spaces eliminated.
1025 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1026 ie = A[Index].end(); it != ie; ++it)
1027 OS << it->getString();
1028 break;
1029 }
1030 }
1031
1032 // Update the scan point.
1033 Body = Body.substr(Pos + 2);
1034 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001035
1036 // We include the .endmacro in the buffer as our queue to exit the macro
1037 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001038 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001039
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001040 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001041}
1042
1043bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1044 const Macro *M) {
1045 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1046 // this, although we should protect against infinite loops.
1047 if (ActiveMacros.size() == 20)
1048 return TokError("macros cannot be nested more than 20 levels deep");
1049
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001050 // Parse the macro instantiation arguments.
1051 std::vector<std::vector<AsmToken> > MacroArguments;
1052 MacroArguments.push_back(std::vector<AsmToken>());
1053 unsigned ParenLevel = 0;
1054 for (;;) {
1055 if (Lexer.is(AsmToken::Eof))
1056 return TokError("unexpected token in macro instantiation");
1057 if (Lexer.is(AsmToken::EndOfStatement))
1058 break;
1059
1060 // If we aren't inside parentheses and this is a comma, start a new token
1061 // list.
1062 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1063 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001064 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001065 // Adjust the current parentheses level.
1066 if (Lexer.is(AsmToken::LParen))
1067 ++ParenLevel;
1068 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1069 --ParenLevel;
1070
1071 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001072 MacroArguments.back().push_back(getTok());
1073 }
1074 Lex();
1075 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001076
1077 // Create the macro instantiation object and add to the current macro
1078 // instantiation stack.
1079 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001080 getTok().getLoc(),
1081 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001082 ActiveMacros.push_back(MI);
1083
1084 // Jump to the macro instantiation and prime the lexer.
1085 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1086 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1087 Lex();
1088
1089 return false;
1090}
1091
1092void AsmParser::HandleMacroExit() {
1093 // Jump to the EndOfStatement we should return to, and consume it.
1094 JumpToLoc(ActiveMacros.back()->ExitLoc);
1095 Lex();
1096
1097 // Pop the instantiation entry.
1098 delete ActiveMacros.back();
1099 ActiveMacros.pop_back();
1100}
1101
Benjamin Kramer38e59892010-07-14 22:38:02 +00001102bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001103 // FIXME: Use better location, we should use proper tokens.
1104 SMLoc EqualLoc = Lexer.getLoc();
1105
Daniel Dunbar821e3332009-08-31 08:09:28 +00001106 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001107 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001108 return true;
1109
Daniel Dunbar3f872332009-07-28 16:08:33 +00001110 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001111 return TokError("unexpected token in assignment");
1112
1113 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001114 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001115
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001116 // Validate that the LHS is allowed to be a variable (either it has not been
1117 // used as a symbol, or it is an absolute symbol).
1118 MCSymbol *Sym = getContext().LookupSymbol(Name);
1119 if (Sym) {
1120 // Diagnose assignment to a label.
1121 //
1122 // FIXME: Diagnostics. Note the location of the definition as a label.
1123 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001124 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1125 ; // Allow redefinitions of undefined symbols only used in directives.
1126 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001127 return Error(EqualLoc, "redefinition of '" + Name + "'");
1128 else if (!Sym->isVariable())
1129 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001130 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001131 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1132 Name + "'");
1133 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001134 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001135
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001136 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001137
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001138 Sym->setUsedInExpr(true);
1139
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001140 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001141 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001142
1143 return false;
1144}
1145
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001146/// ParseIdentifier:
1147/// ::= identifier
1148/// ::= string
1149bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001150 // The assembler has relaxed rules for accepting identifiers, in particular we
1151 // allow things like '.globl $foo', which would normally be separate
1152 // tokens. At this level, we have already lexed so we cannot (currently)
1153 // handle this as a context dependent token, instead we detect adjacent tokens
1154 // and return the combined identifier.
1155 if (Lexer.is(AsmToken::Dollar)) {
1156 SMLoc DollarLoc = getLexer().getLoc();
1157
1158 // Consume the dollar sign, and check for a following identifier.
1159 Lex();
1160 if (Lexer.isNot(AsmToken::Identifier))
1161 return true;
1162
1163 // We have a '$' followed by an identifier, make sure they are adjacent.
1164 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1165 return true;
1166
1167 // Construct the joined identifier and consume the token.
1168 Res = StringRef(DollarLoc.getPointer(),
1169 getTok().getIdentifier().size() + 1);
1170 Lex();
1171 return false;
1172 }
1173
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001174 if (Lexer.isNot(AsmToken::Identifier) &&
1175 Lexer.isNot(AsmToken::String))
1176 return true;
1177
Sean Callanan18b83232010-01-19 21:44:56 +00001178 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001179
Sean Callanan79ed1a82010-01-19 20:22:31 +00001180 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001181
1182 return false;
1183}
1184
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001185/// ParseDirectiveSet:
1186/// ::= .set identifier ',' expression
1187bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001188 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001189
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001190 if (ParseIdentifier(Name))
1191 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001192
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001193 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001194 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001195 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001196
Daniel Dunbare2ace502009-08-31 08:09:09 +00001197 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001198}
1199
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001200bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001201 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001202
1203 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001204 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001205 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1206 if (Str[i] != '\\') {
1207 Data += Str[i];
1208 continue;
1209 }
1210
1211 // Recognize escaped characters. Note that this escape semantics currently
1212 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1213 ++i;
1214 if (i == e)
1215 return TokError("unexpected backslash at end of string");
1216
1217 // Recognize octal sequences.
1218 if ((unsigned) (Str[i] - '0') <= 7) {
1219 // Consume up to three octal characters.
1220 unsigned Value = Str[i] - '0';
1221
1222 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1223 ++i;
1224 Value = Value * 8 + (Str[i] - '0');
1225
1226 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1227 ++i;
1228 Value = Value * 8 + (Str[i] - '0');
1229 }
1230 }
1231
1232 if (Value > 255)
1233 return TokError("invalid octal escape sequence (out of range)");
1234
1235 Data += (unsigned char) Value;
1236 continue;
1237 }
1238
1239 // Otherwise recognize individual escapes.
1240 switch (Str[i]) {
1241 default:
1242 // Just reject invalid escape sequences for now.
1243 return TokError("invalid escape sequence (unrecognized character)");
1244
1245 case 'b': Data += '\b'; break;
1246 case 'f': Data += '\f'; break;
1247 case 'n': Data += '\n'; break;
1248 case 'r': Data += '\r'; break;
1249 case 't': Data += '\t'; break;
1250 case '"': Data += '"'; break;
1251 case '\\': Data += '\\'; break;
1252 }
1253 }
1254
1255 return false;
1256}
1257
Daniel Dunbara0d14262009-06-24 23:30:00 +00001258/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001259/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001260bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001261 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001262 CheckForValidSection();
1263
Daniel Dunbara0d14262009-06-24 23:30:00 +00001264 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001265 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001266 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001267
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001268 std::string Data;
1269 if (ParseEscapedString(Data))
1270 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001271
1272 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001273 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001274 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1275
Sean Callanan79ed1a82010-01-19 20:22:31 +00001276 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001277
1278 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001279 break;
1280
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001281 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001282 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001283 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001284 }
1285 }
1286
Sean Callanan79ed1a82010-01-19 20:22:31 +00001287 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001288 return false;
1289}
1290
1291/// ParseDirectiveValue
1292/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1293bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001294 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001295 CheckForValidSection();
1296
Daniel Dunbara0d14262009-06-24 23:30:00 +00001297 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001298 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001299 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001300 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001301 return true;
1302
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001303 // Special case constant expressions to match code generator.
1304 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001305 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001306 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001307 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001308
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001309 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001310 break;
1311
1312 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001313 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001314 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001315 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001316 }
1317 }
1318
Sean Callanan79ed1a82010-01-19 20:22:31 +00001319 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001320 return false;
1321}
1322
1323/// ParseDirectiveSpace
1324/// ::= .space expression [ , expression ]
1325bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001326 CheckForValidSection();
1327
Daniel Dunbara0d14262009-06-24 23:30:00 +00001328 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001329 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001330 return true;
1331
1332 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001333 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1334 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001335 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001336 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001337
Daniel Dunbar475839e2009-06-29 20:37:27 +00001338 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001339 return true;
1340
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001341 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001342 return TokError("unexpected token in '.space' directive");
1343 }
1344
Sean Callanan79ed1a82010-01-19 20:22:31 +00001345 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001346
1347 if (NumBytes <= 0)
1348 return TokError("invalid number of bytes in '.space' directive");
1349
1350 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001351 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001352
1353 return false;
1354}
1355
1356/// ParseDirectiveFill
1357/// ::= .fill expression , expression , expression
1358bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001359 CheckForValidSection();
1360
Daniel Dunbara0d14262009-06-24 23:30:00 +00001361 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001362 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001363 return true;
1364
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001365 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001366 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001367 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001368
1369 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001370 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001371 return true;
1372
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001373 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001374 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001375 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001376
1377 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001378 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001379 return true;
1380
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001381 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001382 return TokError("unexpected token in '.fill' directive");
1383
Sean Callanan79ed1a82010-01-19 20:22:31 +00001384 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001385
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001386 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1387 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001388
1389 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001390 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001391
1392 return false;
1393}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001394
1395/// ParseDirectiveOrg
1396/// ::= .org expression [ , expression ]
1397bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001398 CheckForValidSection();
1399
Daniel Dunbar821e3332009-08-31 08:09:28 +00001400 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001401 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001402 return true;
1403
1404 // Parse optional fill expression.
1405 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001406 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1407 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001408 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001409 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001410
Daniel Dunbar475839e2009-06-29 20:37:27 +00001411 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001412 return true;
1413
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001414 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001415 return TokError("unexpected token in '.org' directive");
1416 }
1417
Sean Callanan79ed1a82010-01-19 20:22:31 +00001418 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001419
1420 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1421 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001422 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001423
1424 return false;
1425}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001426
1427/// ParseDirectiveAlign
1428/// ::= {.align, ...} expression [ , expression [ , expression ]]
1429bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001430 CheckForValidSection();
1431
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001432 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001433 int64_t Alignment;
1434 if (ParseAbsoluteExpression(Alignment))
1435 return true;
1436
1437 SMLoc MaxBytesLoc;
1438 bool HasFillExpr = false;
1439 int64_t FillExpr = 0;
1440 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001441 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1442 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001443 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001444 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001445
1446 // The fill expression can be omitted while specifying a maximum number of
1447 // alignment bytes, e.g:
1448 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001449 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001450 HasFillExpr = true;
1451 if (ParseAbsoluteExpression(FillExpr))
1452 return true;
1453 }
1454
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001455 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1456 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001457 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001458 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001459
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001460 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001461 if (ParseAbsoluteExpression(MaxBytesToFill))
1462 return true;
1463
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001464 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001465 return TokError("unexpected token in directive");
1466 }
1467 }
1468
Sean Callanan79ed1a82010-01-19 20:22:31 +00001469 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001470
Daniel Dunbar648ac512010-05-17 21:54:30 +00001471 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001472 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001473
1474 // Compute alignment in bytes.
1475 if (IsPow2) {
1476 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001477 if (Alignment >= 32) {
1478 Error(AlignmentLoc, "invalid alignment value");
1479 Alignment = 31;
1480 }
1481
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001482 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001483 }
1484
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001485 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001486 if (MaxBytesLoc.isValid()) {
1487 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001488 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1489 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001490 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001491 }
1492
1493 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001494 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1495 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001496 MaxBytesToFill = 0;
1497 }
1498 }
1499
Daniel Dunbar648ac512010-05-17 21:54:30 +00001500 // Check whether we should use optimal code alignment for this .align
1501 // directive.
1502 //
1503 // FIXME: This should be using a target hook.
1504 bool UseCodeAlign = false;
1505 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001506 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001507 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001508 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1509 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001510 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001511 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001512 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001513 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1514 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001515 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001516
1517 return false;
1518}
1519
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001520/// ParseDirectiveSymbolAttribute
1521/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001522bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001523 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001524 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001525 StringRef Name;
1526
1527 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001528 return TokError("expected identifier in directive");
1529
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001530 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001531
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001532 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001533
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001534 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001535 break;
1536
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001537 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001538 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001539 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001540 }
1541 }
1542
Sean Callanan79ed1a82010-01-19 20:22:31 +00001543 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001544 return false;
1545}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001546
Matt Fleming924c5e52010-05-21 11:36:59 +00001547/// ParseDirectiveELFType
1548/// ::= .type identifier , @attribute
1549bool AsmParser::ParseDirectiveELFType() {
1550 StringRef Name;
1551 if (ParseIdentifier(Name))
1552 return TokError("expected identifier in directive");
1553
1554 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001555 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001556
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001557 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001558 return TokError("unexpected token in '.type' directive");
1559 Lex();
1560
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001561 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001562 return TokError("expected '@' before type");
1563 Lex();
1564
1565 StringRef Type;
1566 SMLoc TypeLoc;
1567
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001568 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001569 if (ParseIdentifier(Type))
1570 return TokError("expected symbol type in directive");
1571
1572 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1573 .Case("function", MCSA_ELF_TypeFunction)
1574 .Case("object", MCSA_ELF_TypeObject)
1575 .Case("tls_object", MCSA_ELF_TypeTLS)
1576 .Case("common", MCSA_ELF_TypeCommon)
1577 .Case("notype", MCSA_ELF_TypeNoType)
1578 .Default(MCSA_Invalid);
1579
1580 if (Attr == MCSA_Invalid)
1581 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1582
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001583 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001584 return TokError("unexpected token in '.type' directive");
1585
1586 Lex();
1587
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001588 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001589
1590 return false;
1591}
1592
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001593/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001594/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1595bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001596 CheckForValidSection();
1597
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001598 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001599 StringRef Name;
1600 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001601 return TokError("expected identifier in directive");
1602
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001603 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001604 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001605
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001606 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001607 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001608 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001609
1610 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001611 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001612 if (ParseAbsoluteExpression(Size))
1613 return true;
1614
1615 int64_t Pow2Alignment = 0;
1616 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001617 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001618 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001619 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001620 if (ParseAbsoluteExpression(Pow2Alignment))
1621 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001622
1623 // If this target takes alignments in bytes (not log) validate and convert.
1624 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1625 if (!isPowerOf2_64(Pow2Alignment))
1626 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1627 Pow2Alignment = Log2_64(Pow2Alignment);
1628 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001629 }
1630
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001631 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001632 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001633
Sean Callanan79ed1a82010-01-19 20:22:31 +00001634 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001635
Chris Lattner1fc3d752009-07-09 17:25:12 +00001636 // NOTE: a size of zero for a .comm should create a undefined symbol
1637 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001638 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001639 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1640 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001641
Eric Christopherc260a3e2010-05-14 01:38:54 +00001642 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001643 // may internally end up wanting an alignment in bytes.
1644 // FIXME: Diagnose overflow.
1645 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001646 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1647 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001648
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001649 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001650 return Error(IDLoc, "invalid symbol redefinition");
1651
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001652 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001653 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001654 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001655 getStreamer().EmitZerofill(Ctx.getMachOSection(
1656 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1657 0, SectionKind::getBSS()),
1658 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001659 return false;
1660 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001661
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001662 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001663 return false;
1664}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001665
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001666/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001667/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001668bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001669 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001670 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001671
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001672 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001673 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001674 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001675
Sean Callanan79ed1a82010-01-19 20:22:31 +00001676 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001677
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001678 if (Str.empty())
1679 Error(Loc, ".abort detected. Assembly stopping.");
1680 else
1681 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001682 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001683
1684 return false;
1685}
Kevin Enderby71148242009-07-14 21:35:03 +00001686
Kevin Enderby1f049b22009-07-14 23:21:55 +00001687/// ParseDirectiveInclude
1688/// ::= .include "filename"
1689bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001690 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001691 return TokError("expected string in '.include' directive");
1692
Sean Callanan18b83232010-01-19 21:44:56 +00001693 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001694 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001695 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001696
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001697 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001698 return TokError("unexpected token in '.include' directive");
1699
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001700 // Strip the quotes.
1701 Filename = Filename.substr(1, Filename.size()-2);
1702
1703 // Attempt to switch the lexer to the included file before consuming the end
1704 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001705 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001706 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001707 return true;
1708 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001709
1710 return false;
1711}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001712
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001713/// ParseDirectiveIf
1714/// ::= .if expression
1715bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001716 TheCondStack.push_back(TheCondState);
1717 TheCondState.TheCond = AsmCond::IfCond;
1718 if(TheCondState.Ignore) {
1719 EatToEndOfStatement();
1720 }
1721 else {
1722 int64_t ExprValue;
1723 if (ParseAbsoluteExpression(ExprValue))
1724 return true;
1725
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001726 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001727 return TokError("unexpected token in '.if' directive");
1728
Sean Callanan79ed1a82010-01-19 20:22:31 +00001729 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001730
1731 TheCondState.CondMet = ExprValue;
1732 TheCondState.Ignore = !TheCondState.CondMet;
1733 }
1734
1735 return false;
1736}
1737
1738/// ParseDirectiveElseIf
1739/// ::= .elseif expression
1740bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1741 if (TheCondState.TheCond != AsmCond::IfCond &&
1742 TheCondState.TheCond != AsmCond::ElseIfCond)
1743 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1744 " an .elseif");
1745 TheCondState.TheCond = AsmCond::ElseIfCond;
1746
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001747 bool LastIgnoreState = false;
1748 if (!TheCondStack.empty())
1749 LastIgnoreState = TheCondStack.back().Ignore;
1750 if (LastIgnoreState || TheCondState.CondMet) {
1751 TheCondState.Ignore = true;
1752 EatToEndOfStatement();
1753 }
1754 else {
1755 int64_t ExprValue;
1756 if (ParseAbsoluteExpression(ExprValue))
1757 return true;
1758
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001759 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001760 return TokError("unexpected token in '.elseif' directive");
1761
Sean Callanan79ed1a82010-01-19 20:22:31 +00001762 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001763 TheCondState.CondMet = ExprValue;
1764 TheCondState.Ignore = !TheCondState.CondMet;
1765 }
1766
1767 return false;
1768}
1769
1770/// ParseDirectiveElse
1771/// ::= .else
1772bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001773 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001774 return TokError("unexpected token in '.else' directive");
1775
Sean Callanan79ed1a82010-01-19 20:22:31 +00001776 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001777
1778 if (TheCondState.TheCond != AsmCond::IfCond &&
1779 TheCondState.TheCond != AsmCond::ElseIfCond)
1780 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1781 ".elseif");
1782 TheCondState.TheCond = AsmCond::ElseCond;
1783 bool LastIgnoreState = false;
1784 if (!TheCondStack.empty())
1785 LastIgnoreState = TheCondStack.back().Ignore;
1786 if (LastIgnoreState || TheCondState.CondMet)
1787 TheCondState.Ignore = true;
1788 else
1789 TheCondState.Ignore = false;
1790
1791 return false;
1792}
1793
1794/// ParseDirectiveEndIf
1795/// ::= .endif
1796bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001797 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001798 return TokError("unexpected token in '.endif' directive");
1799
Sean Callanan79ed1a82010-01-19 20:22:31 +00001800 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001801
1802 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1803 TheCondStack.empty())
1804 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1805 ".else");
1806 if (!TheCondStack.empty()) {
1807 TheCondState = TheCondStack.back();
1808 TheCondStack.pop_back();
1809 }
1810
1811 return false;
1812}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001813
1814/// ParseDirectiveFile
1815/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001816bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001817 // FIXME: I'm not sure what this is.
1818 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001819 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001820 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001821 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001822 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001823
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001824 if (FileNumber < 1)
1825 return TokError("file number less than one");
1826 }
1827
Daniel Dunbareceec052010-07-12 17:45:27 +00001828 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001829 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001830
Chris Lattnerd32e8032010-01-25 19:02:58 +00001831 StringRef Filename = getTok().getString();
1832 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001833 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001834
Daniel Dunbareceec052010-07-12 17:45:27 +00001835 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001836 return TokError("unexpected token in '.file' directive");
1837
Chris Lattnerd32e8032010-01-25 19:02:58 +00001838 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001839 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001840 else {
1841 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1842 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001843 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001844 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001845
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001846 return false;
1847}
1848
1849/// ParseDirectiveLine
1850/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001851bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001852 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1853 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001854 return TokError("unexpected token in '.line' directive");
1855
Sean Callanan18b83232010-01-19 21:44:56 +00001856 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001857 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001858 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001859
1860 // FIXME: Do something with the .line.
1861 }
1862
Daniel Dunbareceec052010-07-12 17:45:27 +00001863 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001864 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001865
1866 return false;
1867}
1868
1869
1870/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001871/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001872/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
1873/// The first number is a file number, must have been previously assigned with
1874/// a .file directive, the second number is the line number and optionally the
1875/// third number is a column position (zero if not specified). The remaining
1876/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001877bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001878
Daniel Dunbareceec052010-07-12 17:45:27 +00001879 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001880 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001881 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001882 if (FileNumber < 1)
1883 return TokError("file number less than one in '.loc' directive");
1884 if (!getContext().ValidateDwarfFileNumber(FileNumber))
1885 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001886 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001887
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001888 int64_t LineNumber = 0;
1889 if (getLexer().is(AsmToken::Integer)) {
1890 LineNumber = getTok().getIntVal();
1891 if (LineNumber < 1)
1892 return TokError("line number less than one in '.loc' directive");
1893 Lex();
1894 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001895
1896 int64_t ColumnPos = 0;
1897 if (getLexer().is(AsmToken::Integer)) {
1898 ColumnPos = getTok().getIntVal();
1899 if (ColumnPos < 0)
1900 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001901 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001902 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001903
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001904 unsigned Flags = 0;
1905 unsigned Isa = 0;
1906 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1907 for (;;) {
1908 if (getLexer().is(AsmToken::EndOfStatement))
1909 break;
1910
1911 StringRef Name;
1912 SMLoc Loc = getTok().getLoc();
1913 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001914 return TokError("unexpected token in '.loc' directive");
1915
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001916 if (Name == "basic_block")
1917 Flags |= DWARF2_FLAG_BASIC_BLOCK;
1918 else if (Name == "prologue_end")
1919 Flags |= DWARF2_FLAG_PROLOGUE_END;
1920 else if (Name == "epilogue_begin")
1921 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
1922 else if (Name == "is_stmt") {
1923 SMLoc Loc = getTok().getLoc();
1924 const MCExpr *Value;
1925 if (getParser().ParseExpression(Value))
1926 return true;
1927 // The expression must be the constant 0 or 1.
1928 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1929 int Value = MCE->getValue();
1930 if (Value == 0)
1931 Flags &= ~DWARF2_FLAG_IS_STMT;
1932 else if (Value == 1)
1933 Flags |= DWARF2_FLAG_IS_STMT;
1934 else
1935 return Error(Loc, "is_stmt value not 0 or 1");
1936 }
1937 else {
1938 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
1939 }
1940 }
1941 else if (Name == "isa") {
1942 SMLoc Loc = getTok().getLoc();
1943 const MCExpr *Value;
1944 if (getParser().ParseExpression(Value))
1945 return true;
1946 // The expression must be a constant greater or equal to 0.
1947 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1948 int Value = MCE->getValue();
1949 if (Value < 0)
1950 return Error(Loc, "isa number less than zero");
1951 Isa = Value;
1952 }
1953 else {
1954 return Error(Loc, "isa number not a constant value");
1955 }
1956 }
1957 else {
1958 return Error(Loc, "unknown sub-directive in '.loc' directive");
1959 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001960
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001961 if (getLexer().is(AsmToken::EndOfStatement))
1962 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001963 }
1964 }
1965
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001966 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,Isa);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001967
1968 return false;
1969}
1970
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001971/// ParseDirectiveMacrosOnOff
1972/// ::= .macros_on
1973/// ::= .macros_off
1974bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1975 SMLoc DirectiveLoc) {
1976 if (getLexer().isNot(AsmToken::EndOfStatement))
1977 return Error(getLexer().getLoc(),
1978 "unexpected token in '" + Directive + "' directive");
1979
1980 getParser().MacrosEnabled = Directive == ".macros_on";
1981
1982 return false;
1983}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001984
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001985/// ParseDirectiveMacro
1986/// ::= .macro name
1987bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
1988 SMLoc DirectiveLoc) {
1989 StringRef Name;
1990 if (getParser().ParseIdentifier(Name))
1991 return TokError("expected identifier in directive");
1992
1993 if (getLexer().isNot(AsmToken::EndOfStatement))
1994 return TokError("unexpected token in '.macro' directive");
1995
1996 // Eat the end of statement.
1997 Lex();
1998
1999 AsmToken EndToken, StartToken = getTok();
2000
2001 // Lex the macro definition.
2002 for (;;) {
2003 // Check whether we have reached the end of the file.
2004 if (getLexer().is(AsmToken::Eof))
2005 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2006
2007 // Otherwise, check whether we have reach the .endmacro.
2008 if (getLexer().is(AsmToken::Identifier) &&
2009 (getTok().getIdentifier() == ".endm" ||
2010 getTok().getIdentifier() == ".endmacro")) {
2011 EndToken = getTok();
2012 Lex();
2013 if (getLexer().isNot(AsmToken::EndOfStatement))
2014 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2015 "' directive");
2016 break;
2017 }
2018
2019 // Otherwise, scan til the end of the statement.
2020 getParser().EatToEndOfStatement();
2021 }
2022
2023 if (getParser().MacroMap.lookup(Name)) {
2024 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2025 }
2026
2027 const char *BodyStart = StartToken.getLoc().getPointer();
2028 const char *BodyEnd = EndToken.getLoc().getPointer();
2029 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2030 getParser().MacroMap[Name] = new Macro(Name, Body);
2031 return false;
2032}
2033
2034/// ParseDirectiveEndMacro
2035/// ::= .endm
2036/// ::= .endmacro
2037bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2038 SMLoc DirectiveLoc) {
2039 if (getLexer().isNot(AsmToken::EndOfStatement))
2040 return TokError("unexpected token in '" + Directive + "' directive");
2041
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002042 // If we are inside a macro instantiation, terminate the current
2043 // instantiation.
2044 if (!getParser().ActiveMacros.empty()) {
2045 getParser().HandleMacroExit();
2046 return false;
2047 }
2048
2049 // Otherwise, this .endmacro is a stray entry in the file; well formed
2050 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002051 return TokError("unexpected '" + Directive + "' in file, "
2052 "no current macro definition");
2053}
2054
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002055void GenericAsmParser::ParseUleb128(uint64_t Value) {
2056 const uint64_t Mask = (1 << 7) - 1;
2057 do {
2058 unsigned Byte = Value & Mask;
2059 Value >>= 7;
2060 if (Value) // Not the last one
2061 Byte |= (1 << 7);
2062 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2063 } while (Value);
2064}
2065
2066void GenericAsmParser::ParseSleb128(int64_t Value) {
2067 const int64_t Mask = (1 << 7) - 1;
2068 for(;;) {
2069 unsigned Byte = Value & Mask;
2070 Value >>= 7;
2071 bool Done = ((Value == 0 && (Byte & 0x40) == 0) ||
2072 (Value == -1 && (Byte & 0x40) != 0));
2073 if (!Done)
2074 Byte |= (1 << 7);
2075 getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
2076 if (Done)
2077 break;
2078 }
2079}
2080
2081bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
2082 int64_t Value;
2083 if (getParser().ParseAbsoluteExpression(Value))
2084 return true;
2085
2086 if (getLexer().isNot(AsmToken::EndOfStatement))
2087 return TokError("unexpected token in directive");
2088
2089 if (DirName[1] == 's')
2090 ParseSleb128(Value);
2091 else
2092 ParseUleb128(Value);
2093 return false;
2094}
2095
2096
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002097/// \brief Create an MCAsmParser instance.
2098MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2099 MCContext &C, MCStreamer &Out,
2100 const MCAsmInfo &MAI) {
2101 return new AsmParser(T, SM, C, Out, MAI);
2102}