blob: 0a664fd876ac35fd77a8cb7d8ef5fbc96a2c0442 [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");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000241 }
242
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000243 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
244 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
245 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000246
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000247 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000248 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
249 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000250};
251
252}
253
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000254namespace llvm {
255
256extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000257extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000258
259}
260
Chris Lattneraaec2052010-01-19 19:46:13 +0000261enum { DEFAULT_ADDRSPACE = 0 };
262
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000263AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
264 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000265 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000266 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000267 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000268 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000269
270 // Initialize the generic parser.
271 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000272
273 // Initialize the platform / file format parser.
274 //
275 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
276 // created.
277 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000278 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000279 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000280 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000281 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000282 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000283 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000284}
285
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000286AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000287 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
288
289 // Destroy any macros.
290 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
291 ie = MacroMap.end(); it != ie; ++it)
292 delete it->getValue();
293
Daniel Dunbare4749702010-07-12 18:12:02 +0000294 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000295 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000296}
297
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000298void AsmParser::PrintMacroInstantiations() {
299 // Print the active macro instantiation stack.
300 for (std::vector<MacroInstantiation*>::const_reverse_iterator
301 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
302 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
303 "note");
304}
305
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000306void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000307 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000308 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000309}
310
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000311bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000312 HadError = true;
Sean Callananbf2013e2010-01-20 23:19:55 +0000313 PrintMessage(L, Msg.str(), "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000314 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000315 return true;
316}
317
Sean Callananbf2013e2010-01-20 23:19:55 +0000318void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
319 const char *Type) const {
320 SrcMgr.PrintMessage(Loc, Msg, Type);
321}
Sean Callananfd0b0282010-01-21 00:19:58 +0000322
323bool AsmParser::EnterIncludeFile(const std::string &Filename) {
324 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
325 if (NewBuf == -1)
326 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000327
Sean Callananfd0b0282010-01-21 00:19:58 +0000328 CurBuffer = NewBuf;
329
330 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
331
332 return false;
333}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000334
335void AsmParser::JumpToLoc(SMLoc Loc) {
336 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
337 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
338}
339
Sean Callananfd0b0282010-01-21 00:19:58 +0000340const AsmToken &AsmParser::Lex() {
341 const AsmToken *tok = &Lexer.Lex();
342
343 if (tok->is(AsmToken::Eof)) {
344 // If this is the end of an included file, pop the parent file off the
345 // include stack.
346 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
347 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000348 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000349 tok = &Lexer.Lex();
350 }
351 }
352
353 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000354 Error(Lexer.getErrLoc(), Lexer.getErr());
Sean Callanan79036e42010-01-20 22:18:24 +0000355
Sean Callananfd0b0282010-01-21 00:19:58 +0000356 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000357}
358
Chris Lattner79180e22010-04-05 23:15:42 +0000359bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000360 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000361 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000362 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000363 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000364 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000365 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
366 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000367
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000368 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000369 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000370
371 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000372 AsmCond StartingCondState = TheCondState;
373
Chris Lattnerb717fb02009-07-02 21:53:43 +0000374 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000375 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000376 if (!ParseStatement()) continue;
377
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000378 // We had an error, validate that one was emitted and recover by skipping to
379 // the next line.
380 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000381 EatToEndOfStatement();
382 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000383
384 if (TheCondState.TheCond != StartingCondState.TheCond ||
385 TheCondState.Ignore != StartingCondState.Ignore)
386 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000387
388 // Check to see there are no empty DwarfFile slots.
389 const std::vector<MCDwarfFile *> &MCDwarfFiles =
390 getContext().getMCDwarfFiles();
391 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000392 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000393 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000394 }
Chris Lattnerb717fb02009-07-02 21:53:43 +0000395
Chris Lattner79180e22010-04-05 23:15:42 +0000396 // Finalize the output stream if there are no errors and if the client wants
397 // us to.
398 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000399 Out.Finish();
400
Chris Lattnerb717fb02009-07-02 21:53:43 +0000401 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000402}
403
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000404void AsmParser::CheckForValidSection() {
405 if (!getStreamer().getCurrentSection()) {
406 TokError("expected section directive before assembly directive");
407 Out.SwitchSection(Ctx.getMachOSection(
408 "__TEXT", "__text",
409 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
410 0, SectionKind::getText()));
411 }
412}
413
Chris Lattner2cf5f142009-06-22 01:29:09 +0000414/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
415void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000416 while (Lexer.isNot(AsmToken::EndOfStatement) &&
417 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000418 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000419
420 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000421 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000422 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000423}
424
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000425StringRef AsmParser::ParseStringToEndOfStatement() {
426 const char *Start = getTok().getLoc().getPointer();
427
428 while (Lexer.isNot(AsmToken::EndOfStatement) &&
429 Lexer.isNot(AsmToken::Eof))
430 Lex();
431
432 const char *End = getTok().getLoc().getPointer();
433 return StringRef(Start, End - Start);
434}
Chris Lattnerc4193832009-06-22 05:51:26 +0000435
Chris Lattner74ec1a32009-06-22 06:32:03 +0000436/// ParseParenExpr - Parse a paren expression and return it.
437/// NOTE: This assumes the leading '(' has already been consumed.
438///
439/// parenexpr ::= expr)
440///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000441bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000442 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000443 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000444 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000445 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000446 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000447 return false;
448}
Chris Lattnerc4193832009-06-22 05:51:26 +0000449
Chris Lattner74ec1a32009-06-22 06:32:03 +0000450/// ParsePrimaryExpr - Parse a primary expression and return it.
451/// primaryexpr ::= (parenexpr
452/// primaryexpr ::= symbol
453/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000454/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000455/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000456bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000457 switch (Lexer.getKind()) {
458 default:
459 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000460 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000461 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000462 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000463 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000464 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000465 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000466 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000467 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000468 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000469 EndLoc = Lexer.getLoc();
470
471 StringRef Identifier;
472 if (ParseIdentifier(Identifier))
473 return false;
474
Daniel Dunbarfffff912009-10-16 01:34:54 +0000475 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000476 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000477 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000478
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000479 // Mark the symbol as used in an expression.
480 Sym->setUsedInExpr(true);
481
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000482 // Lookup the symbol variant if used.
483 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000484 if (Split.first.size() != Identifier.size())
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000485 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
486
Daniel Dunbarfffff912009-10-16 01:34:54 +0000487 // If this is an absolute variable reference, substitute it now to preserve
488 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000489 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000490 if (Variant)
491 return Error(EndLoc, "unexpected modified on variable reference");
492
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000493 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000494 return false;
495 }
496
497 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000498 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000499 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000500 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000501 case AsmToken::Integer: {
502 SMLoc Loc = getTok().getLoc();
503 int64_t IntVal = getTok().getIntVal();
504 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000505 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000506 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000507 // Look for 'b' or 'f' following an Integer as a directional label
508 if (Lexer.getKind() == AsmToken::Identifier) {
509 StringRef IDVal = getTok().getString();
510 if (IDVal == "f" || IDVal == "b"){
511 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
512 IDVal == "f" ? 1 : 0);
513 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
514 getContext());
515 if(IDVal == "b" && Sym->isUndefined())
516 return Error(Loc, "invalid reference to undefined symbol");
517 EndLoc = Lexer.getLoc();
518 Lex(); // Eat identifier.
519 }
520 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000521 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000522 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000523 case AsmToken::Dot: {
524 // This is a '.' reference, which references the current PC. Emit a
525 // temporary label to the streamer and refer to it.
526 MCSymbol *Sym = Ctx.CreateTempSymbol();
527 Out.EmitLabel(Sym);
528 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
529 EndLoc = Lexer.getLoc();
530 Lex(); // Eat identifier.
531 return false;
532 }
533
Daniel Dunbar3f872332009-07-28 16:08:33 +0000534 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000535 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000536 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000537 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000538 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000539 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000540 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000541 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000542 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000543 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000544 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000545 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000546 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000547 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000548 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000549 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000550 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000551 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000552 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000553 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000554 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000555 }
556}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000557
Chris Lattnerb4307b32010-01-15 19:28:38 +0000558bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000559 SMLoc EndLoc;
560 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000561}
562
Chris Lattner74ec1a32009-06-22 06:32:03 +0000563/// ParseExpression - Parse an expression and return it.
564///
565/// expr ::= expr +,- expr -> lowest.
566/// expr ::= expr |,^,&,! expr -> middle.
567/// expr ::= expr *,/,%,<<,>> expr -> highest.
568/// expr ::= primaryexpr
569///
Chris Lattner54482b42010-01-15 19:39:23 +0000570bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000571 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000572 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000573 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
574 return true;
575
576 // Try to constant fold it up front, if possible.
577 int64_t Value;
578 if (Res->EvaluateAsAbsolute(Value))
579 Res = MCConstantExpr::Create(Value, getContext());
580
581 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000582}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000583
Chris Lattnerb4307b32010-01-15 19:28:38 +0000584bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000585 Res = 0;
586 return ParseParenExpr(Res, EndLoc) ||
587 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000588}
589
Daniel Dunbar475839e2009-06-29 20:37:27 +0000590bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000591 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000592
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000593 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000594 if (ParseExpression(Expr))
595 return true;
596
Daniel Dunbare00b0112009-10-16 01:57:52 +0000597 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000598 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000599
600 return false;
601}
602
Daniel Dunbar3f872332009-07-28 16:08:33 +0000603static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000604 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000605 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000606 default:
607 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000608
609 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000610 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000611 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000612 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000613 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000614 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000615 return 1;
616
617 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000618 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000619 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000620 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000621 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000622 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000623 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000624 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000625 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000626 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000627 case AsmToken::ExclaimEqual:
628 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000629 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000630 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000631 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000632 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000633 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000634 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000635 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000636 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000637 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000638 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000639 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000640 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000641 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000642 return 2;
643
644 // Intermediate Precedence: |, &, ^
645 //
646 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000647 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000648 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000649 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000650 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000651 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000652 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000653 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000654 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000655 return 3;
656
657 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000658 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000659 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000660 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000661 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000662 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000663 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000664 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000665 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000666 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000667 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000668 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000669 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000670 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000671 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000672 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000673 }
674}
675
676
677/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
678/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000679bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
680 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000681 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000682 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000683 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000684
685 // If the next token is lower precedence than we are allowed to eat, return
686 // successfully with what we ate already.
687 if (TokPrec < Precedence)
688 return false;
689
Sean Callanan79ed1a82010-01-19 20:22:31 +0000690 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000691
692 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000693 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000694 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000695
696 // If BinOp binds less tightly with RHS than the operator after RHS, let
697 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000698 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000699 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000700 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000701 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000702 }
703
Daniel Dunbar475839e2009-06-29 20:37:27 +0000704 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000705 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000706 }
707}
708
Chris Lattnerc4193832009-06-22 05:51:26 +0000709
710
711
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000712/// ParseStatement:
713/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000714/// ::= Label* Directive ...Operands... EndOfStatement
715/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000716bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000717 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000718 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000719 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000720 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000721 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000722
723 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000724 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000725 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000726 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000727 int64_t LocalLabelVal = -1;
728 // GUESS allow an integer followed by a ':' as a directional local label
729 if (Lexer.is(AsmToken::Integer)) {
730 LocalLabelVal = getTok().getIntVal();
731 if (LocalLabelVal < 0) {
732 if (!TheCondState.Ignore)
733 return TokError("unexpected token at start of statement");
734 IDVal = "";
735 }
736 else {
737 IDVal = getTok().getString();
738 Lex(); // Consume the integer token to be used as an identifier token.
739 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000740 if (!TheCondState.Ignore)
741 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000742 }
743 }
744 }
745 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000746 if (!TheCondState.Ignore)
747 return TokError("unexpected token at start of statement");
748 IDVal = "";
749 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000750
Chris Lattner7834fac2010-04-17 18:14:27 +0000751 // Handle conditional assembly here before checking for skipping. We
752 // have to do this so that .endif isn't skipped in a ".if 0" block for
753 // example.
754 if (IDVal == ".if")
755 return ParseDirectiveIf(IDLoc);
756 if (IDVal == ".elseif")
757 return ParseDirectiveElseIf(IDLoc);
758 if (IDVal == ".else")
759 return ParseDirectiveElse(IDLoc);
760 if (IDVal == ".endif")
761 return ParseDirectiveEndIf(IDLoc);
762
763 // If we are in a ".if 0" block, ignore this statement.
764 if (TheCondState.Ignore) {
765 EatToEndOfStatement();
766 return false;
767 }
768
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000769 // FIXME: Recurse on local labels?
770
771 // See what kind of statement we have.
772 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000773 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000774 CheckForValidSection();
775
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000776 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000777 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000778
779 // Diagnose attempt to use a variable as a label.
780 //
781 // FIXME: Diagnostics. Note the location of the definition as a label.
782 // FIXME: This doesn't diagnose assignment to a symbol which has been
783 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000784 MCSymbol *Sym;
785 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000786 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000787 else
788 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000789 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000790 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000791
Daniel Dunbar959fd882009-08-26 22:13:22 +0000792 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000793 Out.EmitLabel(Sym);
794
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000795 // Consume any end of statement token, if present, to avoid spurious
796 // AddBlankLine calls().
797 if (Lexer.is(AsmToken::EndOfStatement)) {
798 Lex();
799 if (Lexer.is(AsmToken::Eof))
800 return false;
801 }
802
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000803 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000804 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000805
Daniel Dunbar3f872332009-07-28 16:08:33 +0000806 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000807 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000808 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000809
Daniel Dunbare2ace502009-08-31 08:09:09 +0000810 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000811
812 default: // Normal instruction or directive.
813 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000814 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000815
816 // If macros are enabled, check to see if this is a macro instantiation.
817 if (MacrosEnabled)
818 if (const Macro *M = MacroMap.lookup(IDVal))
819 return HandleMacroEntry(IDVal, IDLoc, M);
820
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000821 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000822 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000823 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000824 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000825 return ParseDirectiveSet();
826
Daniel Dunbara0d14262009-06-24 23:30:00 +0000827 // Data directives
828
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000829 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000830 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000831 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000832 return ParseDirectiveAscii(true);
833
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000834 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000835 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000836 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000837 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000838 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000839 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000840 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000841 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000842
Eli Friedman5d68ec22010-07-19 04:17:25 +0000843 if (IDVal == ".align") {
844 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
845 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
846 }
847 if (IDVal == ".align32") {
848 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
849 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
850 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000851 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000852 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000853 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000854 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000855 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000856 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000857 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000858 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000859 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000860 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000861 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000862 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
863
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000864 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000865 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000866
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000867 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000868 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000869 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000870 return ParseDirectiveSpace();
871
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000872 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000873
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000874 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000875 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000876 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000877 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000878 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000879 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000880 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000881 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000882 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000883 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000884 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000885 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000886 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000887 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000888 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000889 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000890 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000891 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000892 if (IDVal == ".type")
893 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000894 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000895 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000896 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000897 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000898 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000899 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000900 if (IDVal == ".weak_def_can_be_hidden")
901 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000902
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000903 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000904 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000905 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000906 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000907
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000908 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000909 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000910 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000911 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000912
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000913 // Look up the handler in the handler table.
914 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
915 DirectiveMap.lookup(IDVal);
916 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000917 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000918
Kevin Enderby9c656452009-09-10 20:51:44 +0000919 // Target hook for parsing target specific directives.
920 if (!getTargetParser().ParseDirective(ID))
921 return false;
922
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000923 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000924 EatToEndOfStatement();
925 return false;
926 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000927
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000928 CheckForValidSection();
929
Chris Lattnera7f13542010-05-19 23:34:33 +0000930 // Canonicalize the opcode to lower case.
931 SmallString<128> Opcode;
932 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
933 Opcode.push_back(tolower(IDVal[i]));
934
Chris Lattner98986712010-01-14 22:21:20 +0000935 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000936 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000937 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000938
Daniel Dunbar3c14ca42010-08-11 06:37:09 +0000939 // Dump the parsed representation, if requested.
940 if (getShowParsedOperands()) {
941 SmallString<256> Str;
942 raw_svector_ostream OS(Str);
943 OS << "parsed instruction: [";
944 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
945 if (i != 0)
946 OS << ", ";
947 ParsedOperands[i]->dump(OS);
948 }
949 OS << "]";
950
951 PrintMessage(IDLoc, OS.str(), "note");
952 }
953
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000954 // If parsing succeeded, match the instruction.
955 if (!HadError) {
956 MCInst Inst;
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000957 if (!getTargetParser().MatchInstruction(IDLoc, ParsedOperands, Inst)) {
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000958 // Emit the instruction on success.
959 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +0000960 } else
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000961 HadError = true;
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000962 }
Chris Lattner98986712010-01-14 22:21:20 +0000963
Chris Lattner98986712010-01-14 22:21:20 +0000964 // Free any parsed operands.
965 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
966 delete ParsedOperands[i];
967
Chris Lattnercbf8a982010-09-11 16:18:25 +0000968 // Don't skip the rest of the line, the instruction parser is responsible for
969 // that.
970 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000971}
Chris Lattner9a023f72009-06-24 04:43:34 +0000972
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000973MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
974 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000975 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
976{
977 // Macro instantiation is lexical, unfortunately. We construct a new buffer
978 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +0000979 SmallString<256> Buf;
980 raw_svector_ostream OS(Buf);
981
982 StringRef Body = M->Body;
983 while (!Body.empty()) {
984 // Scan for the next substitution.
985 std::size_t End = Body.size(), Pos = 0;
986 for (; Pos != End; ++Pos) {
987 // Check for a substitution or escape.
988 if (Body[Pos] != '$' || Pos + 1 == End)
989 continue;
990
991 char Next = Body[Pos + 1];
992 if (Next == '$' || Next == 'n' || isdigit(Next))
993 break;
994 }
995
996 // Add the prefix.
997 OS << Body.slice(0, Pos);
998
999 // Check if we reached the end.
1000 if (Pos == End)
1001 break;
1002
1003 switch (Body[Pos+1]) {
1004 // $$ => $
1005 case '$':
1006 OS << '$';
1007 break;
1008
1009 // $n => number of arguments
1010 case 'n':
1011 OS << A.size();
1012 break;
1013
1014 // $[0-9] => argument
1015 default: {
1016 // Missing arguments are ignored.
1017 unsigned Index = Body[Pos+1] - '0';
1018 if (Index >= A.size())
1019 break;
1020
1021 // Otherwise substitute with the token values, with spaces eliminated.
1022 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1023 ie = A[Index].end(); it != ie; ++it)
1024 OS << it->getString();
1025 break;
1026 }
1027 }
1028
1029 // Update the scan point.
1030 Body = Body.substr(Pos + 2);
1031 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001032
1033 // We include the .endmacro in the buffer as our queue to exit the macro
1034 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001035 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001036
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001037 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001038}
1039
1040bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1041 const Macro *M) {
1042 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1043 // this, although we should protect against infinite loops.
1044 if (ActiveMacros.size() == 20)
1045 return TokError("macros cannot be nested more than 20 levels deep");
1046
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001047 // Parse the macro instantiation arguments.
1048 std::vector<std::vector<AsmToken> > MacroArguments;
1049 MacroArguments.push_back(std::vector<AsmToken>());
1050 unsigned ParenLevel = 0;
1051 for (;;) {
1052 if (Lexer.is(AsmToken::Eof))
1053 return TokError("unexpected token in macro instantiation");
1054 if (Lexer.is(AsmToken::EndOfStatement))
1055 break;
1056
1057 // If we aren't inside parentheses and this is a comma, start a new token
1058 // list.
1059 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1060 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001061 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001062 // Adjust the current parentheses level.
1063 if (Lexer.is(AsmToken::LParen))
1064 ++ParenLevel;
1065 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1066 --ParenLevel;
1067
1068 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001069 MacroArguments.back().push_back(getTok());
1070 }
1071 Lex();
1072 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001073
1074 // Create the macro instantiation object and add to the current macro
1075 // instantiation stack.
1076 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001077 getTok().getLoc(),
1078 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001079 ActiveMacros.push_back(MI);
1080
1081 // Jump to the macro instantiation and prime the lexer.
1082 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1083 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1084 Lex();
1085
1086 return false;
1087}
1088
1089void AsmParser::HandleMacroExit() {
1090 // Jump to the EndOfStatement we should return to, and consume it.
1091 JumpToLoc(ActiveMacros.back()->ExitLoc);
1092 Lex();
1093
1094 // Pop the instantiation entry.
1095 delete ActiveMacros.back();
1096 ActiveMacros.pop_back();
1097}
1098
Benjamin Kramer38e59892010-07-14 22:38:02 +00001099bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001100 // FIXME: Use better location, we should use proper tokens.
1101 SMLoc EqualLoc = Lexer.getLoc();
1102
Daniel Dunbar821e3332009-08-31 08:09:28 +00001103 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001104 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001105 return true;
1106
Daniel Dunbar3f872332009-07-28 16:08:33 +00001107 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001108 return TokError("unexpected token in assignment");
1109
1110 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001111 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001112
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001113 // Validate that the LHS is allowed to be a variable (either it has not been
1114 // used as a symbol, or it is an absolute symbol).
1115 MCSymbol *Sym = getContext().LookupSymbol(Name);
1116 if (Sym) {
1117 // Diagnose assignment to a label.
1118 //
1119 // FIXME: Diagnostics. Note the location of the definition as a label.
1120 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001121 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1122 ; // Allow redefinitions of undefined symbols only used in directives.
1123 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001124 return Error(EqualLoc, "redefinition of '" + Name + "'");
1125 else if (!Sym->isVariable())
1126 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001127 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001128 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1129 Name + "'");
1130 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001131 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001132
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001133 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001134
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001135 Sym->setUsedInExpr(true);
1136
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001137 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001138 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001139
1140 return false;
1141}
1142
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001143/// ParseIdentifier:
1144/// ::= identifier
1145/// ::= string
1146bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001147 // The assembler has relaxed rules for accepting identifiers, in particular we
1148 // allow things like '.globl $foo', which would normally be separate
1149 // tokens. At this level, we have already lexed so we cannot (currently)
1150 // handle this as a context dependent token, instead we detect adjacent tokens
1151 // and return the combined identifier.
1152 if (Lexer.is(AsmToken::Dollar)) {
1153 SMLoc DollarLoc = getLexer().getLoc();
1154
1155 // Consume the dollar sign, and check for a following identifier.
1156 Lex();
1157 if (Lexer.isNot(AsmToken::Identifier))
1158 return true;
1159
1160 // We have a '$' followed by an identifier, make sure they are adjacent.
1161 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1162 return true;
1163
1164 // Construct the joined identifier and consume the token.
1165 Res = StringRef(DollarLoc.getPointer(),
1166 getTok().getIdentifier().size() + 1);
1167 Lex();
1168 return false;
1169 }
1170
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001171 if (Lexer.isNot(AsmToken::Identifier) &&
1172 Lexer.isNot(AsmToken::String))
1173 return true;
1174
Sean Callanan18b83232010-01-19 21:44:56 +00001175 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001176
Sean Callanan79ed1a82010-01-19 20:22:31 +00001177 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001178
1179 return false;
1180}
1181
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001182/// ParseDirectiveSet:
1183/// ::= .set identifier ',' expression
1184bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001185 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001186
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001187 if (ParseIdentifier(Name))
1188 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001189
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001190 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001191 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001192 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001193
Daniel Dunbare2ace502009-08-31 08:09:09 +00001194 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001195}
1196
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001197bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001198 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001199
1200 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001201 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001202 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1203 if (Str[i] != '\\') {
1204 Data += Str[i];
1205 continue;
1206 }
1207
1208 // Recognize escaped characters. Note that this escape semantics currently
1209 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1210 ++i;
1211 if (i == e)
1212 return TokError("unexpected backslash at end of string");
1213
1214 // Recognize octal sequences.
1215 if ((unsigned) (Str[i] - '0') <= 7) {
1216 // Consume up to three octal characters.
1217 unsigned Value = Str[i] - '0';
1218
1219 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1220 ++i;
1221 Value = Value * 8 + (Str[i] - '0');
1222
1223 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1224 ++i;
1225 Value = Value * 8 + (Str[i] - '0');
1226 }
1227 }
1228
1229 if (Value > 255)
1230 return TokError("invalid octal escape sequence (out of range)");
1231
1232 Data += (unsigned char) Value;
1233 continue;
1234 }
1235
1236 // Otherwise recognize individual escapes.
1237 switch (Str[i]) {
1238 default:
1239 // Just reject invalid escape sequences for now.
1240 return TokError("invalid escape sequence (unrecognized character)");
1241
1242 case 'b': Data += '\b'; break;
1243 case 'f': Data += '\f'; break;
1244 case 'n': Data += '\n'; break;
1245 case 'r': Data += '\r'; break;
1246 case 't': Data += '\t'; break;
1247 case '"': Data += '"'; break;
1248 case '\\': Data += '\\'; break;
1249 }
1250 }
1251
1252 return false;
1253}
1254
Daniel Dunbara0d14262009-06-24 23:30:00 +00001255/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001256/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001257bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001258 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001259 CheckForValidSection();
1260
Daniel Dunbara0d14262009-06-24 23:30:00 +00001261 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001262 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001263 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001264
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001265 std::string Data;
1266 if (ParseEscapedString(Data))
1267 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001268
1269 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001270 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001271 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1272
Sean Callanan79ed1a82010-01-19 20:22:31 +00001273 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001274
1275 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001276 break;
1277
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001278 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001279 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001280 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001281 }
1282 }
1283
Sean Callanan79ed1a82010-01-19 20:22:31 +00001284 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001285 return false;
1286}
1287
1288/// ParseDirectiveValue
1289/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1290bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001291 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001292 CheckForValidSection();
1293
Daniel Dunbara0d14262009-06-24 23:30:00 +00001294 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001295 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001296 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001297 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001298 return true;
1299
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001300 // Special case constant expressions to match code generator.
1301 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001302 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001303 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001304 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001305
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001306 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001307 break;
1308
1309 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001310 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001311 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001312 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001313 }
1314 }
1315
Sean Callanan79ed1a82010-01-19 20:22:31 +00001316 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001317 return false;
1318}
1319
1320/// ParseDirectiveSpace
1321/// ::= .space expression [ , expression ]
1322bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001323 CheckForValidSection();
1324
Daniel Dunbara0d14262009-06-24 23:30:00 +00001325 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001326 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001327 return true;
1328
1329 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001330 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1331 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001332 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001333 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001334
Daniel Dunbar475839e2009-06-29 20:37:27 +00001335 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001336 return true;
1337
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001338 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001339 return TokError("unexpected token in '.space' directive");
1340 }
1341
Sean Callanan79ed1a82010-01-19 20:22:31 +00001342 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001343
1344 if (NumBytes <= 0)
1345 return TokError("invalid number of bytes in '.space' directive");
1346
1347 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001348 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001349
1350 return false;
1351}
1352
1353/// ParseDirectiveFill
1354/// ::= .fill expression , expression , expression
1355bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001356 CheckForValidSection();
1357
Daniel Dunbara0d14262009-06-24 23:30:00 +00001358 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001359 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001360 return true;
1361
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001362 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001363 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001364 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001365
1366 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001367 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001368 return true;
1369
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001370 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001371 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001372 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001373
1374 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001375 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001376 return true;
1377
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001378 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001379 return TokError("unexpected token in '.fill' directive");
1380
Sean Callanan79ed1a82010-01-19 20:22:31 +00001381 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001382
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001383 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1384 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001385
1386 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001387 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001388
1389 return false;
1390}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001391
1392/// ParseDirectiveOrg
1393/// ::= .org expression [ , expression ]
1394bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001395 CheckForValidSection();
1396
Daniel Dunbar821e3332009-08-31 08:09:28 +00001397 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001398 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001399 return true;
1400
1401 // Parse optional fill expression.
1402 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001403 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1404 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001405 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001406 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001407
Daniel Dunbar475839e2009-06-29 20:37:27 +00001408 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001409 return true;
1410
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001411 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001412 return TokError("unexpected token in '.org' directive");
1413 }
1414
Sean Callanan79ed1a82010-01-19 20:22:31 +00001415 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001416
1417 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1418 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001419 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001420
1421 return false;
1422}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001423
1424/// ParseDirectiveAlign
1425/// ::= {.align, ...} expression [ , expression [ , expression ]]
1426bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001427 CheckForValidSection();
1428
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001429 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001430 int64_t Alignment;
1431 if (ParseAbsoluteExpression(Alignment))
1432 return true;
1433
1434 SMLoc MaxBytesLoc;
1435 bool HasFillExpr = false;
1436 int64_t FillExpr = 0;
1437 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001438 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1439 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001440 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001441 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001442
1443 // The fill expression can be omitted while specifying a maximum number of
1444 // alignment bytes, e.g:
1445 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001446 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001447 HasFillExpr = true;
1448 if (ParseAbsoluteExpression(FillExpr))
1449 return true;
1450 }
1451
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001452 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1453 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001454 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001455 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001456
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001457 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001458 if (ParseAbsoluteExpression(MaxBytesToFill))
1459 return true;
1460
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001461 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001462 return TokError("unexpected token in directive");
1463 }
1464 }
1465
Sean Callanan79ed1a82010-01-19 20:22:31 +00001466 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001467
Daniel Dunbar648ac512010-05-17 21:54:30 +00001468 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001469 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001470
1471 // Compute alignment in bytes.
1472 if (IsPow2) {
1473 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001474 if (Alignment >= 32) {
1475 Error(AlignmentLoc, "invalid alignment value");
1476 Alignment = 31;
1477 }
1478
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001479 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001480 }
1481
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001482 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001483 if (MaxBytesLoc.isValid()) {
1484 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001485 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1486 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001487 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001488 }
1489
1490 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001491 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1492 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001493 MaxBytesToFill = 0;
1494 }
1495 }
1496
Daniel Dunbar648ac512010-05-17 21:54:30 +00001497 // Check whether we should use optimal code alignment for this .align
1498 // directive.
1499 //
1500 // FIXME: This should be using a target hook.
1501 bool UseCodeAlign = false;
1502 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001503 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001504 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001505 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1506 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001507 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001508 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001509 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001510 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1511 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001512 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001513
1514 return false;
1515}
1516
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001517/// ParseDirectiveSymbolAttribute
1518/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001519bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001520 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001521 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001522 StringRef Name;
1523
1524 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001525 return TokError("expected identifier in directive");
1526
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001527 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001528
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001529 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001530
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001531 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001532 break;
1533
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001534 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001535 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001536 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001537 }
1538 }
1539
Sean Callanan79ed1a82010-01-19 20:22:31 +00001540 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001541 return false;
1542}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001543
Matt Fleming924c5e52010-05-21 11:36:59 +00001544/// ParseDirectiveELFType
1545/// ::= .type identifier , @attribute
1546bool AsmParser::ParseDirectiveELFType() {
1547 StringRef Name;
1548 if (ParseIdentifier(Name))
1549 return TokError("expected identifier in directive");
1550
1551 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001552 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001553
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001554 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001555 return TokError("unexpected token in '.type' directive");
1556 Lex();
1557
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001558 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001559 return TokError("expected '@' before type");
1560 Lex();
1561
1562 StringRef Type;
1563 SMLoc TypeLoc;
1564
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001565 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001566 if (ParseIdentifier(Type))
1567 return TokError("expected symbol type in directive");
1568
1569 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1570 .Case("function", MCSA_ELF_TypeFunction)
1571 .Case("object", MCSA_ELF_TypeObject)
1572 .Case("tls_object", MCSA_ELF_TypeTLS)
1573 .Case("common", MCSA_ELF_TypeCommon)
1574 .Case("notype", MCSA_ELF_TypeNoType)
1575 .Default(MCSA_Invalid);
1576
1577 if (Attr == MCSA_Invalid)
1578 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1579
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001580 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001581 return TokError("unexpected token in '.type' directive");
1582
1583 Lex();
1584
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001585 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001586
1587 return false;
1588}
1589
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001590/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001591/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1592bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001593 CheckForValidSection();
1594
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001595 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001596 StringRef Name;
1597 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001598 return TokError("expected identifier in directive");
1599
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001600 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001601 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001602
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001603 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001604 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001605 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001606
1607 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001608 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001609 if (ParseAbsoluteExpression(Size))
1610 return true;
1611
1612 int64_t Pow2Alignment = 0;
1613 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001614 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001615 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001616 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001617 if (ParseAbsoluteExpression(Pow2Alignment))
1618 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001619
1620 // If this target takes alignments in bytes (not log) validate and convert.
1621 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1622 if (!isPowerOf2_64(Pow2Alignment))
1623 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1624 Pow2Alignment = Log2_64(Pow2Alignment);
1625 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001626 }
1627
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001628 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001629 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001630
Sean Callanan79ed1a82010-01-19 20:22:31 +00001631 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001632
Chris Lattner1fc3d752009-07-09 17:25:12 +00001633 // NOTE: a size of zero for a .comm should create a undefined symbol
1634 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001635 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001636 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1637 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001638
Eric Christopherc260a3e2010-05-14 01:38:54 +00001639 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001640 // may internally end up wanting an alignment in bytes.
1641 // FIXME: Diagnose overflow.
1642 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001643 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1644 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001645
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001646 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001647 return Error(IDLoc, "invalid symbol redefinition");
1648
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001649 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001650 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001651 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001652 getStreamer().EmitZerofill(Ctx.getMachOSection(
1653 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1654 0, SectionKind::getBSS()),
1655 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001656 return false;
1657 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001658
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001659 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001660 return false;
1661}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001662
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001663/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001664/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001665bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001666 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001667 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001668
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001669 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001670 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001671 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001672
Sean Callanan79ed1a82010-01-19 20:22:31 +00001673 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001674
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001675 if (Str.empty())
1676 Error(Loc, ".abort detected. Assembly stopping.");
1677 else
1678 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001679 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001680
1681 return false;
1682}
Kevin Enderby71148242009-07-14 21:35:03 +00001683
Kevin Enderby1f049b22009-07-14 23:21:55 +00001684/// ParseDirectiveInclude
1685/// ::= .include "filename"
1686bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001687 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001688 return TokError("expected string in '.include' directive");
1689
Sean Callanan18b83232010-01-19 21:44:56 +00001690 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001691 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001692 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001693
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001694 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001695 return TokError("unexpected token in '.include' directive");
1696
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001697 // Strip the quotes.
1698 Filename = Filename.substr(1, Filename.size()-2);
1699
1700 // Attempt to switch the lexer to the included file before consuming the end
1701 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001702 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001703 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001704 return true;
1705 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001706
1707 return false;
1708}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001709
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001710/// ParseDirectiveIf
1711/// ::= .if expression
1712bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001713 TheCondStack.push_back(TheCondState);
1714 TheCondState.TheCond = AsmCond::IfCond;
1715 if(TheCondState.Ignore) {
1716 EatToEndOfStatement();
1717 }
1718 else {
1719 int64_t ExprValue;
1720 if (ParseAbsoluteExpression(ExprValue))
1721 return true;
1722
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001723 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001724 return TokError("unexpected token in '.if' directive");
1725
Sean Callanan79ed1a82010-01-19 20:22:31 +00001726 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001727
1728 TheCondState.CondMet = ExprValue;
1729 TheCondState.Ignore = !TheCondState.CondMet;
1730 }
1731
1732 return false;
1733}
1734
1735/// ParseDirectiveElseIf
1736/// ::= .elseif expression
1737bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1738 if (TheCondState.TheCond != AsmCond::IfCond &&
1739 TheCondState.TheCond != AsmCond::ElseIfCond)
1740 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1741 " an .elseif");
1742 TheCondState.TheCond = AsmCond::ElseIfCond;
1743
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001744 bool LastIgnoreState = false;
1745 if (!TheCondStack.empty())
1746 LastIgnoreState = TheCondStack.back().Ignore;
1747 if (LastIgnoreState || TheCondState.CondMet) {
1748 TheCondState.Ignore = true;
1749 EatToEndOfStatement();
1750 }
1751 else {
1752 int64_t ExprValue;
1753 if (ParseAbsoluteExpression(ExprValue))
1754 return true;
1755
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001756 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001757 return TokError("unexpected token in '.elseif' directive");
1758
Sean Callanan79ed1a82010-01-19 20:22:31 +00001759 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001760 TheCondState.CondMet = ExprValue;
1761 TheCondState.Ignore = !TheCondState.CondMet;
1762 }
1763
1764 return false;
1765}
1766
1767/// ParseDirectiveElse
1768/// ::= .else
1769bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001770 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001771 return TokError("unexpected token in '.else' directive");
1772
Sean Callanan79ed1a82010-01-19 20:22:31 +00001773 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001774
1775 if (TheCondState.TheCond != AsmCond::IfCond &&
1776 TheCondState.TheCond != AsmCond::ElseIfCond)
1777 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1778 ".elseif");
1779 TheCondState.TheCond = AsmCond::ElseCond;
1780 bool LastIgnoreState = false;
1781 if (!TheCondStack.empty())
1782 LastIgnoreState = TheCondStack.back().Ignore;
1783 if (LastIgnoreState || TheCondState.CondMet)
1784 TheCondState.Ignore = true;
1785 else
1786 TheCondState.Ignore = false;
1787
1788 return false;
1789}
1790
1791/// ParseDirectiveEndIf
1792/// ::= .endif
1793bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001794 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001795 return TokError("unexpected token in '.endif' directive");
1796
Sean Callanan79ed1a82010-01-19 20:22:31 +00001797 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001798
1799 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1800 TheCondStack.empty())
1801 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1802 ".else");
1803 if (!TheCondStack.empty()) {
1804 TheCondState = TheCondStack.back();
1805 TheCondStack.pop_back();
1806 }
1807
1808 return false;
1809}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001810
1811/// ParseDirectiveFile
1812/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001813bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001814 // FIXME: I'm not sure what this is.
1815 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001816 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001817 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001818 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001819 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001820
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001821 if (FileNumber < 1)
1822 return TokError("file number less than one");
1823 }
1824
Daniel Dunbareceec052010-07-12 17:45:27 +00001825 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001826 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001827
Chris Lattnerd32e8032010-01-25 19:02:58 +00001828 StringRef Filename = getTok().getString();
1829 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001830 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001831
Daniel Dunbareceec052010-07-12 17:45:27 +00001832 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001833 return TokError("unexpected token in '.file' directive");
1834
Chris Lattnerd32e8032010-01-25 19:02:58 +00001835 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001836 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001837 else {
1838 if (getContext().GetDwarfFile(Filename, FileNumber) == 0)
1839 Error(FileNumberLoc, "file number already allocated");
Daniel Dunbareceec052010-07-12 17:45:27 +00001840 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001841 }
Daniel Dunbareceec052010-07-12 17:45:27 +00001842
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001843 return false;
1844}
1845
1846/// ParseDirectiveLine
1847/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001848bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001849 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1850 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001851 return TokError("unexpected token in '.line' directive");
1852
Sean Callanan18b83232010-01-19 21:44:56 +00001853 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001854 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001855 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001856
1857 // FIXME: Do something with the .line.
1858 }
1859
Daniel Dunbareceec052010-07-12 17:45:27 +00001860 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001861 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001862
1863 return false;
1864}
1865
1866
1867/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001868/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001869/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
1870/// The first number is a file number, must have been previously assigned with
1871/// a .file directive, the second number is the line number and optionally the
1872/// third number is a column position (zero if not specified). The remaining
1873/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001874bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001875
Daniel Dunbareceec052010-07-12 17:45:27 +00001876 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001877 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001878 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001879 if (FileNumber < 1)
1880 return TokError("file number less than one in '.loc' directive");
1881 if (!getContext().ValidateDwarfFileNumber(FileNumber))
1882 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001883 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001884
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00001885 int64_t LineNumber = 0;
1886 if (getLexer().is(AsmToken::Integer)) {
1887 LineNumber = getTok().getIntVal();
1888 if (LineNumber < 1)
1889 return TokError("line number less than one in '.loc' directive");
1890 Lex();
1891 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001892
1893 int64_t ColumnPos = 0;
1894 if (getLexer().is(AsmToken::Integer)) {
1895 ColumnPos = getTok().getIntVal();
1896 if (ColumnPos < 0)
1897 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001898 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001899 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001900
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001901 unsigned Flags = 0;
1902 unsigned Isa = 0;
1903 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1904 for (;;) {
1905 if (getLexer().is(AsmToken::EndOfStatement))
1906 break;
1907
1908 StringRef Name;
1909 SMLoc Loc = getTok().getLoc();
1910 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001911 return TokError("unexpected token in '.loc' directive");
1912
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001913 if (Name == "basic_block")
1914 Flags |= DWARF2_FLAG_BASIC_BLOCK;
1915 else if (Name == "prologue_end")
1916 Flags |= DWARF2_FLAG_PROLOGUE_END;
1917 else if (Name == "epilogue_begin")
1918 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
1919 else if (Name == "is_stmt") {
1920 SMLoc Loc = getTok().getLoc();
1921 const MCExpr *Value;
1922 if (getParser().ParseExpression(Value))
1923 return true;
1924 // The expression must be the constant 0 or 1.
1925 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1926 int Value = MCE->getValue();
1927 if (Value == 0)
1928 Flags &= ~DWARF2_FLAG_IS_STMT;
1929 else if (Value == 1)
1930 Flags |= DWARF2_FLAG_IS_STMT;
1931 else
1932 return Error(Loc, "is_stmt value not 0 or 1");
1933 }
1934 else {
1935 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
1936 }
1937 }
1938 else if (Name == "isa") {
1939 SMLoc Loc = getTok().getLoc();
1940 const MCExpr *Value;
1941 if (getParser().ParseExpression(Value))
1942 return true;
1943 // The expression must be a constant greater or equal to 0.
1944 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1945 int Value = MCE->getValue();
1946 if (Value < 0)
1947 return Error(Loc, "isa number less than zero");
1948 Isa = Value;
1949 }
1950 else {
1951 return Error(Loc, "isa number not a constant value");
1952 }
1953 }
1954 else {
1955 return Error(Loc, "unknown sub-directive in '.loc' directive");
1956 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001957
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001958 if (getLexer().is(AsmToken::EndOfStatement))
1959 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001960 }
1961 }
1962
Kevin Enderbyc1840b32010-08-24 20:32:42 +00001963 getContext().setCurrentDwarfLoc(FileNumber, LineNumber, ColumnPos, Flags,Isa);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001964
1965 return false;
1966}
1967
Daniel Dunbar3c802de2010-07-18 18:38:02 +00001968/// ParseDirectiveMacrosOnOff
1969/// ::= .macros_on
1970/// ::= .macros_off
1971bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
1972 SMLoc DirectiveLoc) {
1973 if (getLexer().isNot(AsmToken::EndOfStatement))
1974 return Error(getLexer().getLoc(),
1975 "unexpected token in '" + Directive + "' directive");
1976
1977 getParser().MacrosEnabled = Directive == ".macros_on";
1978
1979 return false;
1980}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001981
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00001982/// ParseDirectiveMacro
1983/// ::= .macro name
1984bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
1985 SMLoc DirectiveLoc) {
1986 StringRef Name;
1987 if (getParser().ParseIdentifier(Name))
1988 return TokError("expected identifier in directive");
1989
1990 if (getLexer().isNot(AsmToken::EndOfStatement))
1991 return TokError("unexpected token in '.macro' directive");
1992
1993 // Eat the end of statement.
1994 Lex();
1995
1996 AsmToken EndToken, StartToken = getTok();
1997
1998 // Lex the macro definition.
1999 for (;;) {
2000 // Check whether we have reached the end of the file.
2001 if (getLexer().is(AsmToken::Eof))
2002 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2003
2004 // Otherwise, check whether we have reach the .endmacro.
2005 if (getLexer().is(AsmToken::Identifier) &&
2006 (getTok().getIdentifier() == ".endm" ||
2007 getTok().getIdentifier() == ".endmacro")) {
2008 EndToken = getTok();
2009 Lex();
2010 if (getLexer().isNot(AsmToken::EndOfStatement))
2011 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2012 "' directive");
2013 break;
2014 }
2015
2016 // Otherwise, scan til the end of the statement.
2017 getParser().EatToEndOfStatement();
2018 }
2019
2020 if (getParser().MacroMap.lookup(Name)) {
2021 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2022 }
2023
2024 const char *BodyStart = StartToken.getLoc().getPointer();
2025 const char *BodyEnd = EndToken.getLoc().getPointer();
2026 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2027 getParser().MacroMap[Name] = new Macro(Name, Body);
2028 return false;
2029}
2030
2031/// ParseDirectiveEndMacro
2032/// ::= .endm
2033/// ::= .endmacro
2034bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2035 SMLoc DirectiveLoc) {
2036 if (getLexer().isNot(AsmToken::EndOfStatement))
2037 return TokError("unexpected token in '" + Directive + "' directive");
2038
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002039 // If we are inside a macro instantiation, terminate the current
2040 // instantiation.
2041 if (!getParser().ActiveMacros.empty()) {
2042 getParser().HandleMacroExit();
2043 return false;
2044 }
2045
2046 // Otherwise, this .endmacro is a stray entry in the file; well formed
2047 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002048 return TokError("unexpected '" + Directive + "' in file, "
2049 "no current macro definition");
2050}
2051
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002052/// \brief Create an MCAsmParser instance.
2053MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2054 MCContext &C, MCStreamer &Out,
2055 const MCAsmInfo &MAI) {
2056 return new AsmParser(T, SM, C, Out, MAI);
2057}